0

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 Answers3

1

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)
Angshu31
  • 1,172
  • 1
  • 8
  • 19
0

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)
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

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