Sometimes, we want to merge 2 arrays of objects with JavaScript.
In this article, we’ll look at how to merge 2 arrays of objects with JavaScript.
How to merge 2 arrays of objects with JavaScript?
To merge 2 arrays of objects with JavaScript, we can use the spread operator.
For instance, we write
const arr1 = new Array(
{ name: "lang", value: "German" },
{ name: "age", value: "33" }
);
const arr2 = new Array(
{ name: "childs", value: "5" },
{ name: "lang", value: "French" }
);
const arr3 = [...arr1, ...arr2];
to merge arr1
and arr2
into one array by spreading the entries of arr1
and arr2
into a new array.
And then we assign that to arr3
.
Conclusion
To merge 2 arrays of objects with JavaScript, we can use the spread operator.