gpt4 book ai didi

javascript - 合并数组中的对象

转载 作者:行者123 更新时间:2023-11-28 11:32:18 25 4
gpt4 key购买 nike

我有两个数组:

[{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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com