Created
October 30, 2014 15:25
-
-
Save amoilanen/ebdf13e626f703c6d5fd to your computer and use it in GitHub Desktop.
Merging several objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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