Skip to content

Instantly share code, notes, and snippets.

@denizozger
Created December 10, 2013 15:15
Show Gist options
  • Save denizozger/7892236 to your computer and use it in GitHub Desktop.
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)
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