Last active
December 19, 2019 01:38
-
-
Save gtkatakura/397a79474f4b5ee6bb22ad49132ebd4c to your computer and use it in GitHub Desktop.
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
const isObject = value => typeof value === 'object' && value !== null; | |
const mergeDeepWithoutLoop = (target, source) => { | |
source = new Proxy(source, { | |
get: function(source, property) { | |
const value = source[property]; | |
if (isObject(value)) { | |
return mergeDeepWithoutLoop(target[property], value); | |
} | |
return value; | |
} | |
}); | |
return { ...target, ...source }; | |
}; | |
const target = { a: { b: { c: 1 }, e: { a: 1 } }, f: 4 }; | |
const source = { a: { b: { d: 2 }, e: { b: 2 } }, g: 4 }; | |
console.log(mergeDeepWithoutLoop(target, source)) // {"a":{"b":{"c":1,"d":2},"e":{"a":1,"b":2}},"f":4,"g":4} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment