Answers for "js create object from array"

5

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

return an object from an array javascript

myArray.find(item => item.isAstronaut)
Posted by: Guest on May-29-2020
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 "js create object from array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language