Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Created July 2, 2014 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosePedroDias/baa3ddc9b5e78ede5849 to your computer and use it in GitHub Desktop.
Save JosePedroDias/baa3ddc9b5e78ede5849 to your computer and use it in GitHub Desktop.
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