我有两个数组:
[{Name: "Jack", Depot: "5"}, {Name: "Jill", Depot: "6"}]
和
[{Depot Name: "Edgware"}, {Depot Name: "Romford"}]
我需要从第二个数组中获取对象并将它们与第一个数组中的对象合并以产生以下结果:
[{Name: "Jack", Depot: "5", Depot Name: "Edgware"}, {Name: "Jill", Depot: "6", Depot Name: "Romford"}]
如有任何帮助,我们将不胜感激
最佳答案
var array1 = [{
Name: "Jack",
Depot: "5"
}, {
Name: "Jill",
Depot: "6"
}];
var array2 = [{
'Depot Name': "Edgware"
}, {
'Depot Name': "Romford"
}];
for (var a in array1) {
for (var p in array1[a]) {
//to esclude all possible internal properties
if (array1[a].hasOwnProperty(p)) {
array2[a][p] = array1[a][p];
}
}
}
console.log(array2);
关于javascript - 合并数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39414773/