Skip to content

Instantly share code, notes, and snippets.

@ricardodantas
Created September 24, 2019 07:10
Show Gist options
  • Save ricardodantas/941342fc1753349c165724c55a7e32e2 to your computer and use it in GitHub Desktop.
Save ricardodantas/941342fc1753349c165724c55a7e32e2 to your computer and use it in GitHub Desktop.
Converting an Array of Objects to an Object
const arrayToObject = (array, keyField) =>
array.reduce((obj, item) => {
return {...obj, [item[keyField]]: item}
}, {});
const peopleObject = arrayToObject(peopleArray, "id");
console.log(peopleObject);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment