Last active
July 11, 2019 15:13
-
-
Save vladanyes/9ab51017705903440c9c520bc8d6f6e4 to your computer and use it in GitHub Desktop.
Deep clone object
This file contains 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
cloneObject = obj => { | |
const clone = {}; | |
for(let i in obj) { | |
if(obj[i] != null && typeof(obj[i]) === "object") { | |
clone[i] = this.cloneObject(obj[i]); | |
} else { | |
clone[i] = obj[i]; | |
} | |
} | |
return clone; | |
}; | |
const deepCloneObject = oldObj => { | |
let newObj = oldObj; | |
if (oldObj && typeof oldObj === 'object') { | |
newObj = Object.prototype.toString.call(oldObj) === "[object Array]" ? [] : {}; | |
for (let i in oldObj) { | |
newObj[i] = deepCloneObject(oldObj[i]); | |
} | |
} | |
return newObj; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment