Skip to content

Instantly share code, notes, and snippets.

@tuvo1106
Created November 20, 2018 20:08
Show Gist options
  • Save tuvo1106/6defec0aa1181eec0ee51760c6595337 to your computer and use it in GitHub Desktop.
Save tuvo1106/6defec0aa1181eec0ee51760c6595337 to your computer and use it in GitHub Desktop.
// deep clone
const deepClone = obj => {
// check to see if argument is an array/object and assign it to new variable
const newObj = (Array.isArray(obj)
? []
: {})
// loop through keys of object OR indexes of array
// Object.entries() work for arrays!
for (const [k, v] of Object.entries(obj)) {
// start deep clone
newObj[k] = (typeof v === 'object')
// if value is another array or object, recursively restart the process
? deepClone(v)
// if not, return value
: v
}
return newObj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment