Skip to content

Instantly share code, notes, and snippets.

@snapwich
Last active June 15, 2019 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snapwich/e20ed4df05a83790dc3135ebead0d193 to your computer and use it in GitHub Desktop.
Save snapwich/e20ed4df05a83790dc3135ebead0d193 to your computer and use it in GitHub Desktop.
let items = [
{name: "something", value: true},
// ...
];
// use reduce to build the new map into an accumulator
// we'll refer to this as "reduce mutate"
let result = items.reduce((acc, item) => {
acc[item.name] = item.value;
return acc;
}, {})
// equivalent as as a for loop
let result = {};
for(let item of items) {
result[item.name] = item.value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment