-1

I have two arrays that I want to merge into each other but I cannot seem to figure out how to NOT override the properties with the same value/id/number.

What I have now:

First array

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"}

Second array

0: {id: 1, title: "Colors", type: "Design"}
1: {id: 2, title: "Typography", type: "Design"}

expected output:

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"}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Adam Schwarcz
  • 437
  • 1
  • 4
  • 14

1 Answers1

1

Those look like arrays, not objects. If so, you can use the concat method to combine them, like so:

let result = [].concat(first, second);

or by using the spread operator, like so:

let result = [...first, ...second];
Jack A.
  • 4,245
  • 1
  • 20
  • 34