Skip to content

Instantly share code, notes, and snippets.

@bitsandbytes
Last active April 12, 2022 10:56
Show Gist options
  • Save bitsandbytes/03766c6ed646ea3737b02b7fc76f3d52 to your computer and use it in GitHub Desktop.
Save bitsandbytes/03766c6ed646ea3737b02b7fc76f3d52 to your computer and use it in GitHub Desktop.
# easy way to remove circular references when stringifying
var cache = [];
JSON.stringify(o, 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment