Categories
JavaScript Answers

How to concatenate two JSON objects with JavaScript?

Spread the love

Sometimes, we want to concatenate two JSON objects with JavaScript.

In this article, we’ll look at how to concatenate two JSON objects with JavaScript.

How to concatenate two JSON objects with JavaScript?

To concatenate two JSON objects with JavaScript, we can use the object spread operator.

For instance, we write

const obj = { ...sourceObj1, ...sourceObj2 };

to put the properties of sourceObj1 and sourceObj2 into the new object after making a shallow copy of the 2 objects.

Then we assign the new object to obj.

Conclusion

To concatenate two JSON objects with JavaScript, we can use the object spread operator.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *