Last active
December 5, 2018 08:14
-
-
Save MKRhere/86f205d7cb591e47c02eb77bd10e9bca to your computer and use it in GitHub Desktop.
remap objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const map = mapper => arr => arr.map(mapper); | |
const reduce = reducer = arr => arr.reduce(reducer); | |
const path = pathSeg => obj => | |
pathSeg.reduce((acc, segment) => acc && acc[segment], obj); | |
/* --------------vy::remap---------------- */ | |
const remap = paths => obj => | |
paths.reduce((acc, [key, seg]) => { | |
acc[key] = path(seg)(obj); | |
return acc; }, {}); | |
/* --------------------------------------- */ | |
const arr = [ | |
{ name: 'a', irrelevant: {} }, | |
{ name: 'b', irrelevant: {} } | |
]; | |
const paths = [ | |
[ 'name', [ 'name' ] ], | |
]; | |
console.log(map(path(['name']))(arr)); | |
console.log(map(remap(paths))(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment