Sometimes, we want to merge two JavaScript objects together in ES6+.
In this article, we’ll look at how to merge two JavaScript objects together in ES6+.
How to merge two JavaScript objects together in ES6+?
To merge two JavaScript objects together in ES6+, we use the object spread syntax.
For instance, we write
const merged = { ...obj1, ...obj2 };
to merge the properties of obj1
and obj2
into a new object.
A shallow copy of obj1
and obj2
are made before their contents are put in the new object.
Conclusion
To merge two JavaScript objects together in ES6+, we use the object spread syntax.