Last active
February 11, 2022 12:06
-
-
Save peeke/5c04191cdee4da6cd3d6cff14f2caedd to your computer and use it in GitHub Desktop.
Deep merge two javascript 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 merge(target, source) { | |
if (!(target instanceof Object) || !(source instanceof Object)) return source | |
return Object.entries(source).reduce( | |
(result, [key, value]) => ({ | |
...result, | |
[key]: merge(result[key], value) | |
}), | |
{ ...target } | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment