Last active
February 17, 2019 20:12
-
-
Save jonaskuske/c7b86473e3b7c70b57b7998bd668796f to your computer and use it in GitHub Desktop.
Recursive Object.assign / merge
This file contains 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 isPlainObject = object => Object.prototype.toString.call(object) === '[object Object]' | |
const recursiveAssign = (target, ...sources) => { | |
sources.forEach(source => { | |
Object.entries(source).forEach(([key, value]) => { | |
if (isPlainObject(target[key]) && isPlainObject(value)) recursiveAssign(target[key], value) | |
else target[key] = value | |
}) | |
}) | |
return target | |
} | |
// Minified + IIFE: | |
const recursiveAssign=(()=>{let c=o=>'[object Object]'==={}.toString.call(o),a=(t,...s)=>(s.forEach(s=>Object.entries(s).forEach(([k,v])=>c(t[k])&&c(v)?a(t[k],v):t[k]=v)),t);return a})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment