如何创建像[['India', 6],['USA', 3]]
使用以下数据
[
{ _id: 'India', count: 6 },
{ _id: 'USA', count: 3 }
]
答案 0 :(得分:1)
您可以使用Array#map
执行此操作
var d = [{
_id: 'India',
count: 6
},
{
_id: 'USA',
count: 3
}
];
var output = d.map(function(ele) {
return [ele._id, ele.count]
});
console.log(output);
如果您对使用ES6的解决方案持开放态度,使用destructuring和arrow functions
会更清晰
let d = [{
_id: 'India',
count: 6
},
{
_id: 'USA',
count: 3
}
];
// Destructure every array element into { _id, count }
// Pass an expression in the RHS that just creates an array using _id and count
let output = d.map(({ _id, count }) => [_id, count])
console.log(output);