make array from object javascript code example

Example 1: convert array of string to array of objects javascript

myArray = myArray.map((str, index) => ({ value: str, id: index + 1 }));

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}

Example 3: converting object to array in js

const numbers = {
  one: 1,
};

const objectArray = Object.entries(numbers);

objectArray.forEach(([key, value]) => {
  console.log(key); // 'one'
  console.log(value); // 1
});

Example 4: how to get array from object in javascript

let result = objArray.map(({ foo }) => foo)