Home > other >  How to get 1 to n key value in an array of objects and push them to a new array?(Javascript)
How to get 1 to n key value in an array of objects and push them to a new array?(Javascript)

Time:12-15

I have an array of objects,

[
 {trial1: 'a1', trial2: 'a2', trial3: 'a3'},
 {trial1: 'b1', trial2: 'b2', trial3: 'b3'},
 {trial1: 'c1', trial2: 'c2', trial3: 'c3'},
 ]

How do I get an array of arrays like this

[
 ['a1','a2','a3'],
 ['b1','b2','b3'],
 ['c1','c2','c3']
]

CodePudding user response:

Use .map(Object.values):

let data = [
 {trial1: 'a1', trial2: 'a2', trial3: 'a3'},
 {trial1: 'b1', trial2: 'b2', trial3: 'b3'},
 {trial1: 'c1', trial2: 'c2', trial3: 'c3'},
];

let result = data.map(Object.values);

console.log(result);

CodePudding user response:

You can do it by using map and Object.values like this:

const data = [
 {trial1: 'a1', trial2: 'a2', trial3: 'a3'},
 {trial1: 'b1', trial2: 'b2', trial3: 'b3'},
 {trial1: 'c1', trial2: 'c2', trial3: 'c3'},
 ];
const values = data.map(Object.values);
console.log(values)

  • Related