Created
May 8, 2018 15:22
-
-
Save jaredwilli/1de0567035e393a7a996a7624adda387 to your computer and use it in GitHub Desktop.
mergeDeep
This file contains hidden or 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
/** | |
* mergeDeep | |
* | |
* @param {*} target object to merge data into | |
* @param {*} source object containing the data to merge | |
*/ | |
export function deepMerge(target, source) { | |
let output = Object.assign({}, target); | |
if (isObject(target) || isObject(source)) { | |
Object.keys(source).forEach((key) => { | |
if (isObject(source[key])) { | |
if (!(key in target)) { | |
Object.assign(output, { | |
[key]: source[key] | |
}); | |
} else { | |
output[key] = deepMerge(target[key], source[key]); | |
} | |
} else { | |
Object.assign(output, { | |
[key]: source[key] | |
}); | |
} | |
}); | |
} | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment