Skip to content

Instantly share code, notes, and snippets.

@mtranter
Created April 4, 2018 05:52
Show Gist options
  • Save mtranter/d1c81c1ccb972a1ecd6e7eb06875fcec to your computer and use it in GitHub Desktop.
Save mtranter/d1c81c1ccb972a1ecd6e7eb06875fcec to your computer and use it in GitHub Desktop.
Like Object.assign. But deep.
const deepAssign = (target, ...sources) =>
sources.reduce((total, src) =>
Object.keys(src).reduce((agg, n) =>
Object.assign({}, agg, {
[n]: (
typeof src[n] === "object" ?
deepAssign(target[n] || {}, src[n]) :
src[n]
)
}),
total),
target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment