Created
December 10, 2013 15:15
-
-
Save denizozger/7892236 to your computer and use it in GitHub Desktop.
Log a Javascript object preventing circular structure error (a minor amendment to http://stackoverflow.com/a/9653082)
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 testObject = { | |
id : 5, | |
name : 'foo' | |
} | |
function censor(censor) { | |
return (function() { | |
var i = 0; | |
return function(key, value) { | |
if(i !== 0 && typeof(censor) === 'object' && typeof(value) == 'object' && censor == value) | |
return '[Circular]'; | |
if(i >= 29) // seems to be a harded maximum of 30 serialized objects? | |
return '[Unknown]'; | |
++i; // so we know we aren't using the original object anymore | |
return value; | |
} | |
})(censor); | |
} | |
console.log(JSON.stringify(testObject, censor(testObject), 4)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment