Skip to content

Instantly share code, notes, and snippets.

@MKRhere
Last active December 5, 2018 08:14
Show Gist options
  • Save MKRhere/86f205d7cb591e47c02eb77bd10e9bca to your computer and use it in GitHub Desktop.
Save MKRhere/86f205d7cb591e47c02eb77bd10e9bca to your computer and use it in GitHub Desktop.
remap objects
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