build object from array javascript code example
Example 1: js array into object
const names = ['Alex', 'Bob', 'Johny', 'Atta'];
// convert array to th object
const obj = Object.assign({}, names);
// print object
console.log(obj);
// {0: "Alex", 1: "Bob", 2: "Johny", 3: "Atta"}
Example 2: js create object from array
[
{ id: 10, color: "red" },
{ id: 20, color: "blue" },
{ id: 30, color: "green" }
].reduce((acc, cur) => ({ ...acc, [cur.color]: cur.id }), {})
//output:
{red: 10, blue: 20, green: 30}