Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Created May 12, 2015 11:19
Show Gist options
  • Save amatiasq/c66ae4ab8c3ced08155a to your computer and use it in GitHub Desktop.
Save amatiasq/c66ae4ab8c3ced08155a to your computer and use it in GitHub Desktop.
Deep extend for simple JSON objects
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