Answers for "js create object as array"

3

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}
Posted by: Guest on January-04-2021
0

js create object from array

const obj = {};

 for (const key of yourArray) {
      obj[key] = whatever;
 }
Posted by: Guest on September-29-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language