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
  • 64,414
  • 37
  • 100
  • 175
Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69

2 Answers2

7

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,409
  • 8
  • 11
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
  • 6,038
  • 19
  • 75
  • 149
rajabraza
  • 333
  • 3
  • 11