Skip to content

Instantly share code, notes, and snippets.

@CodinCat
Created August 15, 2016 08:31
Show Gist options
  • Save CodinCat/d2bdc4b721efc8bba01c8c2662b2e15b to your computer and use it in GitHub Desktop.
Save CodinCat/d2bdc4b721efc8bba01c8c2662b2e15b to your computer and use it in GitHub Desktop.
Deep clone
function deepClone(obj) {
if (Array.isArray(obj))
return obj.map((el) => deepClone(el))
if (
typeof obj !== 'object' ||
Object.prototype.toString.call(obj) !== '[object Object]' ||
obj === null
)
return obj
let clone = {}
for (let i in obj)
if (obj.hasOwnProperty(i))
clone[i] = deepClone(obj[i])
return clone
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment