Created
May 23, 2024 13:09
-
-
Save ArtemSites/f9c109da993661e714054fa83e2f7a5a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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