Skip to content

Instantly share code, notes, and snippets.

@AhmedBHameed
Last active March 14, 2019 14:00
Show Gist options
  • Save AhmedBHameed/99e706cfadc96d1ccace5507e027b3da to your computer and use it in GitHub Desktop.
Save AhmedBHameed/99e706cfadc96d1ccace5507e027b3da to your computer and use it in GitHub Desktop.
Helper function for JavaScript ES6

Those are ES6 helper functions

ArrayToObjects or ArrayToEntities

let arrayToObj = (array, keyField) => Object.assign({}, ...array.map( item => ({[item[keyField]]: item}) ));

let arrayOfObj = [
    {id: 10, name: "Ahmed", age: 29},
    {id: 11, name: "Mohameed", age: 36}
];

console.log(arrayToObj(arrayOfObj, "id"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment