Skip to content

Instantly share code, notes, and snippets.

@sidouglas
Created January 8, 2020 11:15
Show Gist options
  • Save sidouglas/168af7536cb48da6e578f7375fd52a91 to your computer and use it in GitHub Desktop.
Save sidouglas/168af7536cb48da6e578f7375fd52a91 to your computer and use it in GitHub Desktop.
Console.log for a JSON.Stringify Circular reference
var cache = [];
var x = JSON.stringify(YOUR_VARIABLE_OBJECT_HERE, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
});
cache = null; // Enable garbage collection
console.log(x);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment