Skip to content

Instantly share code, notes, and snippets.

@mhingston
Created June 3, 2016 10:04
Show Gist options
  • Save mhingston/cbfb9e36af72effcb50ae40a11f9e294 to your computer and use it in GitHub Desktop.
Save mhingston/cbfb9e36af72effcb50ae40a11f9e294 to your computer and use it in GitHub Desktop.
Deeply merge 2 objects, i.e. deep Object.assign
const mergeObjects = (target, source) =>
{
const isObject = item => item && typeof item === "object" && !Array.isArray(item) && item !== null
if(isObject(target) && isObject(source))
{
for(let key in source)
{
if(source.hasOwnProperty(key) && isObject(source[key]))
{
if(!target.hasOwnProperty(key))
{
target[key] = {}
}
mergeObjects(target[key], source[key])
}
else
{
target[key] = source[key]
}
}
}
return target
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment