Answers for "how can i create an object from 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

create object from array

// create object from array
const createObjectFromArray = (arr) => {
  let obj = {};
  for (let value of arr) {
    obj[value] = ++obj[value] || 1;
  }
  return obj;
};
Posted by: Guest on December-09-2021

Code answers related to "how can i create an object from array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language