Skip to content

Instantly share code, notes, and snippets.

@manojsinghnegiwd
Created October 19, 2016 18:32
Show Gist options
  • Save manojsinghnegiwd/394b5307b5cefab0f9a9962952ba744e to your computer and use it in GitHub Desktop.
Save manojsinghnegiwd/394b5307b5cefab0f9a9962952ba744e to your computer and use it in GitHub Desktop.
Function to merge object in javascript and return a new merged object
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