Skip to content

Instantly share code, notes, and snippets.

@gtkatakura
Last active December 19, 2019 01:38
Show Gist options
  • Save gtkatakura/397a79474f4b5ee6bb22ad49132ebd4c to your computer and use it in GitHub Desktop.
Save gtkatakura/397a79474f4b5ee6bb22ad49132ebd4c to your computer and use it in GitHub Desktop.
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