我有这三组数组:
var name = ["john", "peter", "rick"];
var age = [20, 45, 30];
var country = ["Brazil", "USA", "Italy"];
我需要组合成单个对象数组并获得以下结果:
var result = [{
'name':'John',
'age':20,
'country': 'Brazil'},
{
'name':'peter',
'age':45,
'country': 'USA'},
{
'name':'Rick',
'age':30,
'country': 'Italy'}
];
答案 0 :(得分:0)
如果它们的顺序相同,则可以执行以下操作:
var firstname = ["john", "peter", "rick"];
var age = [20, 45, 30];
var country = ["Brazil", "USA", "Italy"];
var array = [];
for (var i = 0; i < firstname.length; i++) {
array.push({firstname: firstname[i], age: age[i], country: country[i]});
}
console.log( JSON.stringify(array) );
请注意,我将name
更改为firstname
:由于某种原因,它不适用于name
,可能是因为它是Javascript或浏览器中的保留术语。>