简体   繁体   中英

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

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)

 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)

您可以使用 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)]})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM