3

I need to convert a object

{score: 77, id: 166}

to an array,

[77,166]

I tried,

Object.keys(obj).map((key) => [obj[key]]);

but seems like, it returns as 2 arrays.

[[77][166]]
Lajos Arpad
  • 53,986
  • 28
  • 88
  • 159
Sai Krishnadas
  • 1,717
  • 3
  • 18
  • 42

2 Answers2

6

You just had an extra pair of square brackets in your code

const obj = {score: 77, id: 166};
const result = Object.keys(obj).map((key) => obj[key]);

console.log(result)
ahsan
  • 1,344
  • 6
  • 10
3

You can use also use Object.values(obj) to achieve this result

const obj = {
  score: 77,
  id: 166
}
const result = Object.values(obj)

console.log(result);

this will return you an array of values

demo
  • 5,676
  • 16
  • 65
  • 141
rajabraza
  • 305
  • 3
  • 9