Create an object from a given array

Javascript

Create an object from a given array

Use this function to convert objects to an array of objects

javascript

function objectFromArray(arr, key = 'id') { if (arr && arr.length) { return arr.reduce((v, i) => { v[i[key]] = i; return v; }, {}); } return {}; }
Usage example:
const people = [ { "id": "123", "name": "John" } ]; console.log(objectFromArray(people)); // prints { "123": { "id": "123", "name": "John" } }