Created
October 4, 2014 04:30
-
-
Save alexandervasyuk/afbd60384d9cde2e7d49 to your computer and use it in GitHub Desktop.
Merge Two 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
/* | |
Write a function that will recursively merge two objects with the following conditions: | |
1.) If a[field] is an array, and b[field] is defined and is not an array, add b[field] to the array | |
2.) If a[field] is an array an b[field] exists but is undefined or null, set a[field] to an empty array | |
3.) If a[filed] is an array and b[field] is an array, set a[field] to b[field] | |
4.) If a[field] exists and b[field] exists but is undefined, delete a[field] | |
5.) If b[field] is a non-complex type (number, string, boolean, et cetera), copy to a[field] | |
*/ | |
var a = { | |
first_name: 'Bob', | |
last_name: 'Jones', | |
email: '[email protected]', | |
address: { | |
line_1: '1234 Main St', | |
line_2: 'Apt 413', | |
city: 'Los Angeles', | |
state: 'CA', | |
zip: '90048' | |
}, | |
logins: [ | |
{ date: '10/22/2012', ip: '192.168.0.1' }, | |
{ date: '10/21/2012', ip: '192.168.0.1' } | |
], | |
photos: [ | |
'IMG-1985.jpg', | |
'IMG-1987.jpg' | |
] | |
} | |
var b = { | |
last_name: 'Jones', | |
active: true, | |
address: { | |
line_1: '2143 South Main St', | |
line_2: undefined | |
}, | |
logins: { date: '10/23/2012', ip: '192.168.0.1' }, | |
photos: undefined | |
} | |
var result = { | |
first_name: 'Bob', | |
last_name: 'Jones', | |
active: true, | |
email: '[email protected]', | |
address: { | |
line_1: '2143 South Main St', | |
city: 'Los Angeles', | |
state: 'CA', | |
zip: '90048' | |
}, | |
logins: [ | |
{ date: '10/22/2012', ip: '192.168.0.1' }, | |
{ date: '10/21/2012', ip: '192.168.0.1' }, | |
{ date: '10/23/2012', ip: '192.168.0.1' } | |
], | |
photos: [] | |
} | |
//merges b into a | |
var merge = function(a, b) { | |
for (var field in b) { | |
if (a.hasOwnProperty(field)) { | |
if (a[field] instanceof Array) { | |
//If a[field] is an array, and b[field] is defined and is not an array, add b[field] to the array | |
if (typeof b[field] != "undefined" && !(b[field] instanceof Array)) { | |
a[field].push(b[field]) | |
} else if (typeof b[field] === "undefined" || b[field] == null) { //If a[field] is an array an b[field] exists but is undefined or null, set a[field] to an empty array | |
a[field] = new Array(); | |
} else if (b[field] instanceof Array) { //If a[field] is an array and b[field] is an array, set a[field] to b[field] | |
a[field] = b[field]; | |
} | |
} else if (b[field] == undefined) { //If a[field] exists and b[field] exists but is undefined, delete a[field] | |
delete a[field]; | |
} else if (!(b[field] instanceof Object)) { //If b[field] is a non-complex type (number, string, boolean, et cetera), copy to a[field] | |
a[field] = b[field]; | |
} else if (b[field] != null && b[field] instanceof Object) { | |
merge(a[field], b[field]); | |
} | |
} else { | |
a[field] = b[field]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment