How To Merge Two Arrays Of Objects In JavaScript

Hello devs,

In this tutorial, I will show you how to merge two arrays into one array in javascript. To merge arrays of objects, javascript provides Array.prototype.push.apply(arr1,arr2). We can use this method to combine two arrays of objects. Sometimes we need to combine or merge two arrays of objects javascript.

So let's see how we can combine two arrays of objects in javascript. 

var a =  [
                {
                    name: "Mahedi", 
                    age: "28"
                },
                { 
                    name: "Hafiz", 
                    value: "27"
                }
            ];

var b =  [
                {
                    name: "Foysal", 
                    age: "28"
                },
                { 
                    name: "Zamil", 
                    value: "27"
                }
            ];

Array.prototype.push.apply(a,b); 

 

Remember that, the final merged result will be in a. Hope it can help you.

 

#javascript