3 Ways To Merge Objects In Javascript (Simple Examples)

Welcome to a tutorial on how to merge or combine two objects together in Javascript. Having trouble with objects? Combining objects together sure ain’t as easy as concatenating strings together.

We can merge objects together in Javascript using one of the following methods:

  1. Use the spread operator –  var combined = {...objA, ...objB};
  2. Use the object assign function – var combined = Object.assign(objA, objB);
  3. Manually loop and combine –
    • var combined = {};
    • for (var key in objA) { combined[key] = objA[key]; }
    • for (var key in objB) { combined[key] = objB[key]; }

That covers the basics, but let us walk through some examples in this guide – Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Combine Objects Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

WAYS TO COMBINE OBJECTS

All right, let us now get into the examples on the various ways to combine objects in Javascript.

 

1) SPREAD OPERATOR

1-spread.html
// (A) OBJECTS TO BE COMBINED
var objA = {id: 999, name: "John Doe"};
var objB = {email: "john@doe.com"};
var objC = {gender: "Male"};
 
// (B) COMBINE 2 OBJECTS
var combined = {...objA, ...objB};
console.log(combined);
 
// (C) WE CAN COMBINE MORE THAN 2 OBJECTS USING SPREAD
var combined = {...objA, ...objB, ...objC};
console.log(combined);

For the beginners who have not heard of it – The spread operator (...) “opens” up an iterable object and “plugs” the individual elements into where you need them to be. Might be kind of confusing at first, but as you can see, it is also extremely convenient in many ways.

Take extra note though, the spread operator is introduced in ES2018. Don’t be too quick to adopt this if you have to support legacy browsers; Not everyone keeps their browser up to date.

 

 

2) OBJECT ASSIGN FUNCTION

2-object-assign.html
// (A) OBJECTS TO BE COMBINED
var objA = {id: 999, name: "John Doe"};
var objB = {email: "john@doe.com"};

// (B) COMBINE OBJECTS
var combined = Object.assign(objA, objB);
console.log(combined);

// (C) OBJECT ASSIGN CAN ALSO COMBINE MULTIPLE OBJECTS AT ONCE
var objC = {foo: "bar"};
var objD = {hello: "world"};
var combined = Object.assign(objA, objB, objC, objD);
console.log(combined);

The Object.assign() function is another convenient one to know. Introduced in ES2015, it should be widely implemented and supported by now.

 

 

3) MANUAL LOOP

3-manual-loop.html
// (A) OBJECTS TO BE COMBINED
var objA = {id: 999, name: "John Doe"};
var objB = {email: "john@doe.com"};

// (B) MANUALLY LOOP AND COMBINE
var combined = {};
for (var key in objA) {
  combined[key] = objA[key];
}
for (var key in objB) {
  combined[key] = objB[key];
}
console.log(combined);

Once upon a time, we don’t have the convenience of the spread operator ... and Object.assign(). This is the old school way of how we combine objects – Manually looping through and “copying” them.

 

 

USEFUL BITS & LINKS

That’s all for this guide, and here is a small section on some extras that may be useful to you.

 

WHICH IS THE BEST METHOD?

Personally, I am leaning towards Object.assign(), which is convenient and widely supported. But if you have to do some “data yoga” and restructuring while combining the objects, the manual loop is the only way to go. So summary, all 3 methods work. Use whichever makes the most sense for your project.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

How to Merge Objects in Javascript (Click to Enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.