Search code examples
javascriptobjectecmascript-6arrayofarrays

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


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);


Solution

  • 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)