Created
July 15, 2020 10:57
-
-
Save JakeChampion/db625cb7bde5528f1154f88674638d43 to your computer and use it in GitHub Desktop.
replacer function for json.stringify to change circular references
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 circularReferenceReplacer () { | |
const seen = new WeakMap(); | |
return (key, value) => { | |
if (typeof value === "object" && value !== null) { | |
if (seen.has(value)) { | |
let path = seen.get(value); | |
if (path === "") { | |
path = "the root object"; | |
} | |
return 'circular reference which points to ' + path + '.'; | |
} | |
seen.set(value, key); | |
} | |
return value; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment