Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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) {
v = '[removed]';
else {
return v;
return JSON.parse(s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment