Created
January 8, 2020 11:15
-
-
Save sidouglas/168af7536cb48da6e578f7375fd52a91 to your computer and use it in GitHub Desktop.
Console.log for a JSON.Stringify Circular reference
This file contains hidden or 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 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