Skip to content

Instantly share code, notes, and snippets.

@mgtitimoli
Last active March 15, 2016 21:37
Show Gist options
  • Save mgtitimoli/09569485bff9d99c8d26 to your computer and use it in GitHub Desktop.
Save mgtitimoli/09569485bff9d99c8d26 to your computer and use it in GitHub Desktop.
Transform object: entries > filter > map > reduce (entries > destination object)
function objectSetEntry(object, [ key, value ]) {
object[key] = value;
return object;
}
export default function transformObject(
source,
filterEntries = undefined,
mapEntries = undefined,
destination = {}
) {
let entries = Object.entries(source);
if (filterEntries) {
entries = entries.filter(filterEntries);
}
if (mapEntries) {
entries = entries.map(mapEntries);
}
return entries.reduce(
objectSetEntry,
destination
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment