Pre-recorded question

Question #46
In JS, how to convert objects to arrays?
Answer

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

5 events in history
Answer by Alphonsio 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 Alphonsio 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 Alphonsio 06/06/2020 at 11:54:08 AM
In JS, how to convert objects to arrays?
Question by Alphonsio 06/06/2020 at 11:53:50 AM
In JavaScript, how to convert objects to arrays?
Question by Alphonsio 06/06/2020 at 11:53:34 AM
How to convert JavaScript objects to arrays?
# ID Query URL Count

Icons proudly provided by Friconix.