Skip to content

Instantly share code, notes, and snippets.

@amoilanen
Created October 30, 2014 15:25
Show Gist options
  • Save amoilanen/ebdf13e626f703c6d5fd to your computer and use it in GitHub Desktop.
Save amoilanen/ebdf13e626f703c6d5fd to your computer and use it in GitHub Desktop.
Merging several objects
function merge() {
var sources = [].slice.call(arguments);
return sources.reduce(function(previous, source) {
Object.keys(source).forEach(function(propertyName) {
previous[propertyName] = source[propertyName];
});
return previous;
}, {});
}
var obj1 = {
'key1': 'value1',
'key2': 'value2',
'key3': 'value3'
};
var obj2 = {
'key3': 'value3',
'key4': 'value4',
'key5': 'value5'
};
var obj3 = {
'key6': 'value6'
}
merge(obj1, obj2, obj3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment