create array from object javascript code example

Example 1: javascript object to array

//ES6 Object to Array

const numbers = {
  one: 1,
  two: 2,
};

console.log(Object.values(numbers));
// [ 1, 2 ]

console.log(Object.entries(numbers));
// [ ['one', 1], ['two', 2] ]

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: js array

const users = ["Mark", "Greg"];

Example 4: how to get array from object in javascript

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

Example 5: javascript to array

Array.from("Hello"); // ["H", "e", "l", "l", "o"]

Tags:

Java Example