Created
October 21, 2019 03:36
-
-
Save krnbr/a8f1f283cda02b558acb255d36c5632b to your computer and use it in GitHub Desktop.
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
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