Created
August 15, 2016 08:31
-
-
Save CodinCat/d2bdc4b721efc8bba01c8c2662b2e15b to your computer and use it in GitHub Desktop.
Deep clone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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