Skip to content

Instantly share code, notes, and snippets.

@vladanyes
Last active July 11, 2019 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladanyes/9ab51017705903440c9c520bc8d6f6e4 to your computer and use it in GitHub Desktop.
Save vladanyes/9ab51017705903440c9c520bc8d6f6e4 to your computer and use it in GitHub Desktop.
Deep clone object
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