我有两个数组想要相互合并,但我似乎无法弄清楚如何不覆盖具有相同值/id/编号的属性。
我现在拥有的:
第一个数组
0: {id: 1, title: "Button", type: "Controls & Inputs"}
1: {id: 2, title: "Switch", type: "Selection Controls"}
2: {id: 3, title: "Tags", type: "Controls & Inputs"}
3: {id: 4, title: "Checkbox", type: "Selection Controls"}
4: {id: 5, title: "Toast", type: "Notifications & Alerts"}
第二个数组
0: {id: 1, title: "Colors", type: "Design"}
1: {id: 2, title: "Typography", type: "Design"}
预期输出:
0: {id: 1, title: "Button", type: "Controls & Inputs"}
1: {id: 2, title: "Switch", type: "Selection Controls"}
2: {id: 3, title: "Tags", type: "Controls & Inputs"}
3: {id: 4, title: "Checkbox", type: "Selection Controls"}
4: {id: 5, title: "Toast", type: "Notifications & Alerts"}
5: {id: 1, title: "Colors", type: "Design"}
6: {id: 2, title: "Typography", type: "Design"}
答案 0 :(得分:1)
那些看起来像数组,而不是对象。如果是这样,您可以使用 concat
方法将它们组合起来,如下所示:
let result = [].concat(first, second);
或使用展开运算符,如下所示:
let result = [...first, ...second];