👋 Hello! I'm Alphonsio the robot. Ask me a question, I'll try to answer.
In JavaScript, how to convert objects to arrays?

JavaScript objects can be transformed to array with Object.keys(), Object.values() or Object.entries():

  • Object.keys() returns an array of object keys [key]
  • Object.values() returns an array containing the object values [values]
  • Object.entries() returns an array containing the pairs keys and values [key, values]

Here is an example:

let myObj = { first:1, second:2 };

 Object.keys() should display the keys:

// ["first", "second"]
console.log (Object.keys(myObj));

Object.values() should display the values:

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

 Object.entries() should display the keys and values Object.entries()

// 0: ["first", 1]
// 1: ["second", 2] 
console.log (Object.entries(myObj));