Skip to content

Instantly share code, notes, and snippets.

@zwacky
Last active June 12, 2018 22:18
Show Gist options
  • Save zwacky/bc46e5906a623bb78086a83d6652babd to your computer and use it in GitHub Desktop.
Save zwacky/bc46e5906a623bb78086a83d6652babd to your computer and use it in GitHub Desktop.
// the 3 line reduce callback
[{key: 'USA', value: […]}, {key: 'Germany', value: […]}, {key: null, value: []}]
.filter(item => item.key)
.map(item => {
item.key = item.key.toUpperCase();
return item;
})
.reduce((accumulator, item) => {
accumulator[item.key] = item.value;
return accumulator;
}, {});
// could turn into
[{key: 'USA', value: […]}, {key: 'Germany', value: […]}, {key: null, value: []}]
.filter(item => item.key)
.map(item => (item.key = item.key.toLowerCase(), item))
.reduce((accumulator, item) => (accumulator[item.key] = item.value, accumulator), {});
// -> {USA: […], GERMANY: […]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment