gpt4 book ai didi

javascript - 如何组合两个在Javascript中具有相同值的数组

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

我有数组

 var array1 = [
{"id" : 1,"val1" : 1,"val2": 1},
{"id" : 2,"val1" : 2,"val2": 1},
{"id" : 3,"val1" : 0,"val2": 0}
];

var array2 = [
{"id" : 1,"val1" : 1,"val3": 1},
{"id" : 2,"val3": 1},
{"id" : 3,"val3": 0}
];

我希望输出是这样的

 var array3 = [
{"id" : 1,"val1" : 1,"val2": 1, "val3" : 1},
{"id" : 2,"val1" : 2, "val3" : 1},
{"id" : 3,"val1" : 0, "val3" : 0}
];

有没有简单的(r)快捷方式?谢谢!

最佳答案

您可以使用.map() , Object.assign()

var array3 = array1.map(function(o, i) {
return Object.assign({}, array2[i], o)
})

关于javascript - 如何组合两个在Javascript中具有相同值的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42215611/

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