-
-
Save WhatIsLoveBaev/e81a668871d292dda4560e7ed6e3ba3a to your computer and use it in GitHub Desktop.
Deep-Merge JavaScript objects with ES6
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
| // Merge a `source` object to a `target` recursively | |
| const merge = (target, source) => { | |
| // Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties | |
| for (const key of Object.keys(source)) { | |
| if (source[key] instanceof Object) Object.assign(source[key], merge(target[key], source[key])) | |
| } | |
| // Join `target` and modified `source` | |
| Object.assign(target || {}, source) | |
| return target | |
| } |
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
| const merge=(t,s)=>{const o=Object,a=o.assign;for(const k of o.keys(s))s[k]instanceof o&&a(s[k],merge(t[k],s[k]));return a(t||{},s),t} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment