Skip to content

Instantly share code, notes, and snippets.

@krnbr
Created October 21, 2019 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krnbr/a8f1f283cda02b558acb255d36c5632b to your computer and use it in GitHub Desktop.
Save krnbr/a8f1f283cda02b558acb255d36c5632b to your computer and use it in GitHub Desktop.
function JSONStringify(object) {
var cache:any[] = [];
var str = JSON.stringify(object,
// custom replacer fxn - gets around "TypeError: Converting circular structure to JSON"
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;
}, 4);
cache = [];
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment