To clone a JS Object- Json
Will return a copy of a JSON with all the nested objects
by Ishank Dubey
JavaScript
var anObj = {species:'mamal',
'subSpecies':{'tail':1,'ears':'2','etc':{a:'banana'}},
'monkey':{'bananas':'Eat'}
};
var anObj1 = anObj;
var anObj3 = clone(anObj);
function clone(theObj){
var outObj = {};
for(var prop in theObj){
if(theObj.hasOwnProperty(prop)){
if(typeof theObj[prop] == 'object' && Object.keys(theObj[prop])){
outObj[prop] = clone(theObj[prop]);
}else{
outObj[prop] = theObj[prop];
}
}
}
return outObj;
}
console.log(anObj);
console.log(anObj1);
console.log(anObj3);