Last active
March 15, 2016 21:37
-
-
Save mgtitimoli/09569485bff9d99c8d26 to your computer and use it in GitHub Desktop.
Transform object: entries > filter > map > reduce (entries > destination object)
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
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