What’s the best/standard way of merging two associative arrays in JavaScript? Does everyone just do it by rolling their own for loop?
-
11there are no associative arrays in javascript btw, only objects.gblazex– gblazex2010-07-05 21:25:25 +00:00Commented Jul 5, 2010 at 21:25
-
Crossref same question in Perl: stackoverflow.com/questions/350018/…dreftymac– dreftymac2014-07-28 20:38:02 +00:00Commented Jul 28, 2014 at 20:38
-
Associative arrays in javascript: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Chris Martin– Chris Martin2016-01-29 21:40:01 +00:00Commented Jan 29, 2016 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 JavaScript
(associative arrays are objects in JavaScript)
Look here: http://api.jquery.com/jQuery.extend/
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.
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 JavaScript
If working with ES6, this can be achieved with the spread operator:
const obj3 = { ...obj1, ...obj2 };
4 Comments
obj1, use an empty object as target: $.extend({}, obj1, obj2)Object.assign(), which doesn't rely on JQuery if you're not using the library already.Now in 2016 I would say the best/standard way is Object.assign().
In pure JavaScript, without any 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 on Object.assign().
1 Comment
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;
};
It is called as, for example:
var arr1 = { robert: "bobby", john: "jack" };
var arr2 = { elizabeth: "liz", jennifer: "jen" };
var shortnames = Object.extend(arr1, arr2);
1 Comment
Keep it simple...
function mergeArray(array1,array2) {
for(item in array1) {
array2[item] = array1[item];
}
return array2;
}
2 Comments
arrau1.prototypehasOwnProperty 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 which has already been corrected.Underscore.js 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}
Comments
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.
Comments
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!
Comments
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];
}
}
}
}
Comments
- In JavaScript, there isn't any notion of associative arrays. 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
1 Comment
Yahoo UI (YUI) also has a helper function for this:
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);
Comments
jQuery has a Boolean thingamajiggy for deep copy. You could do something like this:
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);
Comments
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;
}
Comments
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 }
2 Comments
Object.getOwnPropertyNames is not a function will crash the app.