Skip to content

Instantly share code, notes, and snippets.

@artemsites
Created May 23, 2024 13:09
Show Gist options
  • Save artemsites/f9c109da993661e714054fa83e2f7a5a to your computer and use it in GitHub Desktop.
Save artemsites/f9c109da993661e714054fa83e2f7a5a to your computer and use it in GitHub Desktop.
export function objectMergeDeep(target, source) {
Object.keys(source).forEach(key => {
const targetValue = target[key];
const sourceValue = source[key];
if (typeof sourceValue === 'object' && sourceValue !== null) {
if (typeof targetValue === 'object' && targetValue !== null) {
objectMergeDeep(targetValue, sourceValue);
} else {
target[key] = (Array.isArray(sourceValue)) ? [] : {};
objectMergeDeep(target[key], sourceValue);
}
} else {
target[key] = sourceValue;
}
});
return target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment