Created
October 19, 2016 18:32
-
-
Save manojsinghnegiwd/394b5307b5cefab0f9a9962952ba744e to your computer and use it in GitHub Desktop.
Function to merge object in javascript and return a new merged object
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
var merge = function() { | |
var object = {}; | |
for( var i = 0; i < arguments.length; i++ ) { | |
for(var prop in arguments[i]) { | |
object[prop] = arguments[i][prop]; | |
} | |
} | |
return object; | |
} | |
// now use it like this | |
var obj1 = { | |
a: 1, | |
b: 2 | |
} | |
var obj2 = { | |
a: 3, | |
j: 5, | |
} | |
var obj3 = { | |
props: { | |
x: 2, | |
msg: 'Hello' | |
}, | |
p: 5, | |
} | |
var new_object = merge(obj1, obj2, obj3); | |
console.log(new_object); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment