public
Last active

Javascript object deep copy

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
function processObjWithRef(obj, result){
if(obj==null || typeof obj != 'object'){
//nothing really to do here - you're going to lose the reference to result if you try an assignment
}
if(obj instanceof Array) {
for(var i=0; i<obj.length; i++){
result.push();
processObjWithRef(obj[i], result[i]);
}
}
if(obj instanceof Object){
for(var k in obj){
var count=0;
if(obj[k]==null || typeof obj[k] != 'object'){
result[k] = obj[k];
}else if(obj[k] instanceof Array) {
result[k] = [];
processObjWithRef(obj[k], result[k]);
}else if(obj[k] instanceof Object){
result[k] = {};
for( var attr in obj[k]){
processObjWithRef(obj[k], result[k]);
}
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.