Home > other >  convert a key value object to a single array [duplicate]
convert a key value object to a single array [duplicate]

Time:09-28

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]]

CodePudding user response:

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)

CodePudding user response:

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

  • Related