How can I convert Array of Objects into Array of Array of Array

79 Views Asked by At

Hello Everyone I'm beginner in javascript I'm trying to convert from Array of objects into Array of array.I have tried some methods like Object.entries.But I didn't get the output what I expected.If anyone helps It will really helpful to me.Any kind of help would be appreciated.Thanks in advance....

My Input:

  var data=[
           {name:'TOYA',price:34},
           {name:'TOYB',price:24},
           {name:'TOYC',price:444},
           {name:'TOYD',price:54}
        ];


Expected Output:

   var data=[
           ['TOYA',34],
           ['TOYB',24],
           ['TOYC',444],
           ['TOYD',54]
        ];
     
 But I got:
 
[ [ '0', { name: 'TOYA', price: 34 } ],
  [ '1', { name: 'TOYB', price: 24 } ],
  [ '2', { name: 'TOYC', price: 444 } ],
  [ '3', { name: 'TOYD', price: 54 } ] ]
  
 using Object.entries(data);

3

There are 3 best solutions below

1
On BEST ANSWER

Use Object.values instead.

var data=[
     {name:'TOYA',price:34},
     {name:'TOYB',price:24},
     {name:'TOYC',price:444},
     {name:'TOYD',price:54}
];

var newdata = [];
for (let obj of data) {
  newdata.push(Object.values(obj));
}
console.log(newdata)

2
On

var data=[
           {name:'c++',price:34},
           {name:'java',price:24},
           {name:'python',price:444},
           {name:'php',price:54}
        ];


var result = Object.values(data).map(v => Object.values(v));
console.log(result)

0
On

You can use Object.values(arrayElement)

[{name:'c++',price:34},{name:'java',price:24},{name:'python',price:444},       {name:'php',price:54}].map((e)=>{return [Object.values(e)]})