我有一个像这样的对象数组:
var myArray = [
{
name: "abc",
age: 23,
rank:208
},
{
name: "pqr",
age: 25,
rank:178
},
{
name: "xyz",
age: 21,
rank:108
}
];
我期待的结果:
var newArray = [['abc', 23, 208], ['pqr', 25, 178], ['xyz', 21, 108]];
我试图找到解决方案,但不知道在互联网上搜索什么。请帮忙。提前谢谢。
答案 0 :(得分:3)
使用地图,这样可以简洁明了。
var newArray = myArray.map(function (item) {
return Object.keys(item).map(function (subitem) {
return item[subitem]
});
});
或者如果您正好使用ES2015(较新版本的JavaScript):
let newArray = myArray.map(item => Object.values(item));
答案 1 :(得分:1)
你可以试试这样的......
var myArray = [{
name: "abc",
age: 23,
rank: 208
}, {
name: "pqr",
age: 25,
rank: 178
}, {
name: "xyz",
age: 21,
rank: 108
}];
function convertToArrayOfArrays(arr) {
var newArray = [];
for (var i = 0; i < arr.length; i++) {
var arraySection = [];
var obj = arr[i];
for (var key in obj) {
arraySection.push(obj[key]);
}
newArray.push(arraySection);
}
return newArray;
}
console.log(convertToArrayOfArrays(myArray));
答案 2 :(得分:-1)
var result = [];
for (i in myArray){
var newArray = [];
for(j in myArray[i]){
newArray.push(myArray[i][j]);
}
result.push(newArray);
}