Skip to content

Instantly share code, notes, and snippets.

@maulberto3
Last active July 12, 2018 04:01
Show Gist options
  • Save maulberto3/fe14535122802a4b76f7c831100231e0 to your computer and use it in GitHub Desktop.
Save maulberto3/fe14535122802a4b76f7c831100231e0 to your computer and use it in GitHub Desktop.
Reduce example #4
const arr = [["dog", 1], ["cat", 2], ["bear", 3]];
const func1 = ([key, v1]) => ({ [key]: v1 }); // creates an object out of the array
const func2 = (obj, prop) => Object.assign(obj, prop); // assigns each object to an accumulator object
const obj3 = arr.map(func1).reduce(func2); // nesting
console.log(JSON.stringify(obj3, null, 2)); // Prints { "dog": 1, "cat": 2, "bear": 3 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment