Created
May 12, 2015 11:19
-
-
Save amatiasq/c66ae4ab8c3ced08155a to your computer and use it in GitHub Desktop.
Deep extend for simple JSON 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 deepExtend(target, source) { | |
Object.keys(source).forEach(function(key) { | |
var value = source[key]; | |
var dest = target[key]; | |
var sourceType = typeof value; | |
var destType = typeof target[key]; | |
if (Array.isArray(value) && Array.isArray(dest)) | |
target[key] = dest.concat(value); | |
else if (sourceType === destType && sourceType === 'object') | |
deepExtend(dest, value); | |
else | |
target[key] = value; | |
}); | |
return target; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment