Question #45   Submitted by Answiki on 06/06/2020 at 11:54:08 AM UTC

In JS, how to convert objects to arrays?

Answer   Submitted by Answiki on 02/23/2022 at 07:46:55 AM UTC

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:

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

6 events in history
Answer by Answiki on 02/23/2022 at 07:46:55 AM

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:

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

Answer by Answiki on 06/22/2020 at 06:04:17 AM

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));

Answer by Answiki on 06/06/2020 at 12:03:07 PM

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));

Question by Answiki 06/06/2020 at 11:54:08 AM
In JS, how to convert objects to arrays?
Question by Answiki 06/06/2020 at 11:53:50 AM
In JavaScript, how to convert objects to arrays?
Question by Answiki 06/06/2020 at 11:53:34 AM
How to convert JavaScript objects to arrays?
# ID Query URL Count

Icons proudly provided by Friconix.