What’s the best/standard way of merging two associative arrays in JavaScript? Does everyone just do it by rolling their own for
loop?

- 18,263
- 7
- 55
- 75

- 6,506
- 5
- 30
- 26
-
11there are no associative arrays in javascript btw, only objects. – gblazex Jul 05 '10 at 21:25
-
Crossref same question in Perl: http://stackoverflow.com/questions/350018/how-can-i-combine-hashes-in-perl/350038#350038 – dreftymac Jul 28 '14 at 20:38
-
Associative arrays in javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map – Chris Martin Jan 29 '16 at 21:40
16 Answers
with jquery you can call $.extend
var obj1 = {a: 1, b: 2};
var obj2 = {a: 4, c: 110};
var obj3 = $.extend(obj1, obj2);
obj1 == obj3 == {a: 4, b: 2, c: 110} // Pseudo JS
(assoc. arrays are objects in js)
look here: http://api.jquery.com/jQuery.extend/
edit: Like rymo suggested, it's better to do it this way:
obj3 = $.extend({}, obj1, obj2);
obj3 == {a: 4, b: 2, c: 110}
As here obj1 (and obj2) remain unchanged.
edit2: In 2018 the way to do it is via Object.assign
:
var obj3 = Object.assign({}, obj1, obj2);
obj3 === {a: 4, b: 2, c: 110} // Pseudo JS
If working with ES6 this can be achieved with the Spread Operator:
const obj3 = { ...obj1, ...obj2 };

- 3,244
- 2
- 25
- 23
-
55or to avoid mucking `obj1`, use an empty object as target: `$.extend({}, obj1, obj2)` – rymo Mar 19 '12 at 18:37
-
Yes, when dealing with closures and objects passed by reference, take rymo's advice to avoid affecting the original object for subsequent operations. – Nathan Smith Aug 15 '12 at 13:48
-
2For modern browsers, check the solution by @manfred using `Object.assign()`, which doesn't rely on JQuery if you're not using the library already. – Phil May 23 '18 at 10:48
-
It works, but it alters the first parameter and that's something you need to be aware of. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign – kulpae Jul 12 '19 at 09:52
Now in 2016 I would say the best/standard way is Object.assign()
Pure Javascript. No jQuery is needed.
obj1 = {a: 1, b: 2};
obj2 = {a: 4, c: 110};
obj3 = Object.assign({},obj1, obj2); // Object {a: 4, b: 2, c: 110}
More information, examples and polyfill here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

- 990
- 9
- 10
-
What is the type for obj3? what if obj1 is type IABC, will obj3 keep that type afterwards? – windmaomao Mar 07 '18 at 17:23
This is how Prototype does it:
Object.extend = function(destination, source) {
for (var property in source) {
if (source.hasOwnProperty(property)) {
destination[property] = source[property];
}
}
return destination;
};
called as, for example:
var arr1 = { robert: "bobby", john: "jack" };
var arr2 = { elizabeth: "liz", jennifer: "jen" };
var shortnames = Object.extend(arr1,arr2);
EDIT: added hasOwnProperty() check as correctly pointed out by bucabay in comments

- 56,385
- 11
- 85
- 79
-
6You'll need the test source.hasOwnProperty(property) to make sure you only copy the immediate properties over. As it is, this could will copy all properties including those derived from Object.prototype – bucabay Sep 20 '09 at 18:54
Keep it simple...
function mergeArray(array1,array2) {
for(item in array1) {
array2[item] = array1[item];
}
return array2;
}

- 227
- 2
- 2
-
7You to check hasOwnProperty to avoid copying any properties on the `arrau1.prototype` – Lee Goddard Sep 06 '14 at 19:33
-
@LeeGee is right. You need that `hasOwnProperty` check. Referring to the objects as arrays is also misleading (they're objects), the arg names should convey that array2 is mutated or a new object should be returned. I would edit the answer but in effect it would become almost exactly [Jonathan Fingland's edited answer](http://stackoverflow.com/a/929791/1064996) which has already been corrected. – Vaz Feb 13 '16 at 00:08
Underscore also has an extend method:
Copy all of the properties in the source objects over to the destination object. It's in-order, so the last source will override properties of the same name in previous arguments.
_.extend(destination, *sources)
_.extend({name : 'moe'}, {age : 50});
=> {name : 'moe', age : 50}

- 8,424
- 6
- 52
- 67

- 2,388
- 1
- 29
- 32
In dojo, the 2-objects/arrays "merge" would be lang.mixin(destination, source)
-- you can also mix multiple sources into one destination, etc -- see the mixin function's reference for details.

- 3,788
- 3
- 39
- 68

- 854,459
- 170
- 1,222
- 1,395
Rolling Your Own Extend/Mixin Function
function extend(objects) {
var args
, first = Array.prototype.slice.call(arguments, 0, 1)[0]
, second;
if (arguments.length > 1) {
second = Array.prototype.splice.call(arguments, 1, 1)[0];
for (var key in second) {
first[key] = second[key];
}
args = Array.prototype.slice.call(arguments, 0);
return extend.apply(this, args);
}
return first;
}
...
var briansDirections = {
step1: 'Remove pastry from wrapper.',
step2: 'Place pastry toaster.',
step3: 'Remove pastry from toaster and enjoy.',
};
extend(briansDirections, { step1: 'Toast Poptarts' }, { step2: 'Go ahead, toast \'em' }, { step3: 'Hey, are you sill reading this???' });
...
This simply extends a splat of objects, recursively. Also, note that this recursive function is TCO (Tail-Call Optimized) as its return is the last call to itself.
Additionally, you may want targeted properties. In this case, you may want to condense objects based upon id
, quantity
, or another property. This approach could have a small book written about it and requires object-juxtaposition and can get very complex. I've written a small library for this which is available upon request.
Hope this helps!
do you want to overwrite a property if the names are the same but the values are not?
And do you want to permanently change one of the original objects,
or do you want a new merged object returned?
function mergedObject(obj1, obj2, force){
for(var p in obj1) this[p]= obj1[p];
for(var p in obj2){
if(obj2.hasOwnProperty(p)){
if(force || this[p]=== undefined) this[p]= obj2[p];
else{
n= 2;
while(this[p+n]!== undefined)++n;
this[p+n]= obj2[p];
}
}
}
}

- 102,654
- 32
- 106
- 127
- In Javascript there is no notion of associative array, there are objects
- The only way to merge two objects is to loop for their properties and copy pointers to their values that are not primitive types and values for primitive types to another instance

- 31,255
- 9
- 54
- 56
-
8Objects in Javascript are implemented as associative arrays, so there certainly is the notion of same – Dexygen May 30 '09 at 14:16
jquery has a boolean for deep copy. You could do something like that:
MergeRecursive = function(arr1, arr2){
$.extend(true, arr1, arr2);
return arr1;
};
Also you can edit this function to support n-arrays to merge.
ArrayMergeRecursive = function(){
if(arguments.length < 2){
throw new Error("ArrayMergeRecursive: Please enter two or more objects to merge!");
}
var arr1=arguments[0];
for(var i=0; i<=arguments.length; i++ ){
$.extend(true, arr1, arguments[i]);
}
return arr1;
};
So now you can do
var arr1 = {'color': {'mycolor': 'red'}, 3: 5},
arr2 = {4: 10, 'color': {'favorite': 'green', 0: 'blue'}},
arr3 = ['Peter','Jhon','Demosthenes'],
results = ArrayMergeRecursive(arr1, arr2, arr3); // (arr1, arr2 ... arrN)
console.log("Result is:", results);

- 5,978
- 4
- 38
- 80

- 1,151
- 1
- 13
- 17
Yahoo UI (YUI) also has a helper function for this:
http://developer.yahoo.com/yui/examples/yahoo/yahoo_merge.html
YAHOO.namespace('example');
YAHOO.example.set1 = { foo : "foo" };
YAHOO.example.set2 = { foo : "BAR", bar : "bar" };
YAHOO.example.set3 = { foo : "FOO", baz : "BAZ" };
var Ye = YAHOO.example;
var merged = YAHOO.lang.merge(Ye.set1, Ye.set2, Ye.set3);

- 4,006
- 2
- 30
- 28
I needed a deep-object-merging. So all of the other answers didn't help me very much. _.extend and jQuery.extend do well, unless you have a recursive array like i do. But it ain't so bad, you can program it in five minutes:
var deep_merge = function (arr1, arr2) {
jQuery.each(arr2, function (index, element) {
if (typeof arr1[index] === "object" && typeof element === "object") {
arr1[index] = deep_merge(arr1[index], element);
} else if (typeof arr1[index] === "array" && typeof element === "array") {
arr1[index] = arr1[index].concat(element);
} else {
arr1[index] = element;
}
});
return arr1;
}

- 71
- 1
- 1
To merge arrays in jQuery what about $.merge?
var merged = $.merge([{id:3, value:'foo3'}], [{id:1, value:'foo1'}, {id:2, value:'foo2'}]);
merged[0].id == 3;
merged[0].value == 'foo3';
merged[1].id == 1;
merged[1].value == 'foo1';
merged[2].id == 2;
merged[2].value == 'foo2';

- 586
- 8
- 21
Recursive solution (extends also arrays of objects) + null checked
var addProps = function (original, props) {
if(!props) {
return original;
}
if (Array.isArray(original)) {
original.map(function (e) {
return addProps(e, props)
});
return original;
}
if (!original) {
original = {};
}
for (var property in props) {
if (props.hasOwnProperty(property)) {
original[property] = props[property];
}
}
return original;
};
Tests
console.log(addProps([{a: 2}, {z: 'ciao'}], {timestamp: 13}));
console.log(addProps({single: true}, {timestamp: 13}));
console.log(addProps({}, {timestamp: 13}));
console.log(addProps(null, {timestamp: 13}));
[ { a: 2, timestamp: 13 }, { z: 'ciao', timestamp: 13 } ]
{ single: true, timestamp: 13 }
{ timestamp: 13 }
{ timestamp: 13 }

- 5,938
- 5
- 42
- 54
-
I tried using this with Node JS, but `Object.getOwnPropertyNames is not a function` will crash the app. – Legoless May 17 '16 at 09:54
-
I'm using this with some oldish v8 engine within plv8 PostgreSQL extension. And it works in browsers. Just find a way in your JS engine to do the equivalent thing as "hasOwnProperty". Then you can reuse this logic. – sscarduzio May 17 '16 at 16:04