Deep copy of objects
by Anchit Gupta
JavaScript
let obj = {
id : 1,
details : {
name: "Anchit",
age : 26,
printName : function(){
console.log("my name is: ", this.name);
}
},
other : {
random : {
type: "text"
}
}
};
const deepCopyFunc = (inObj) =>{
let outObj, key, value;
if (typeof inObj === null || typeof inObj !== "object"){
return inObj;
}
outObj = Array.isArray(inObj) ? [] : {};
for (key in inObj){
value = inObj[key];
outObj[key] = deepCopyFunc(value);
}
return outObj;
}
let obj2 = deepCopyFunc(obj);
console.log(obj2);