receives a JS object which may have cycles and returns another one with repeated cycles replaced by '[removed]' string. Use return instead if you don't mind them not being signalled.
var removeCycles = function(o) { | |
var seen = []; | |
var s = JSON.stringify(o, function(k, v) { | |
if (v !== null && typeof v === 'object') { | |
if (seen.indexOf(v) !== -1) { | |
//return; | |
v = '[removed]'; | |
} | |
else { | |
seen.push(v); | |
} | |
} | |
return v; | |
}); | |
return JSON.parse(s); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment