Created
July 2, 2014 14:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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