Skip to content

Instantly share code, notes, and snippets.

@kijart
Created November 17, 2020 09:54
Show Gist options
  • Save kijart/bb2c10e7f46102ba202f8ddbb1fa190e to your computer and use it in GitHub Desktop.
Save kijart/bb2c10e7f46102ba202f8ddbb1fa190e to your computer and use it in GitHub Desktop.
JavaScript deep merge
// Ref: https://stackoverflow.com/a/34749873
/**
* Simple object check.
* @param item
* @returns {boolean}
*/
export function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
/**
* Deep merge two objects.
* @param target
* @param ...sources
*/
export function mergeDeep(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return mergeDeep(target, ...sources);
}
// usage
mergeDeep(this, { a: { b: { c: 123 } } });
// or
const merged = mergeDeep({a: 1}, { b : { c: { d: { e: 12345}}}});
console.dir(merged); // { a: 1, b: { c: { d: [Object] } } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment