Skip to content

Instantly share code, notes, and snippets.

@jonaskuske
Last active February 17, 2019 20:12
Show Gist options
  • Save jonaskuske/c7b86473e3b7c70b57b7998bd668796f to your computer and use it in GitHub Desktop.
Save jonaskuske/c7b86473e3b7c70b57b7998bd668796f to your computer and use it in GitHub Desktop.
Recursive Object.assign / merge
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