Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JakeChampion/db625cb7bde5528f1154f88674638d43 to your computer and use it in GitHub Desktop.
Save JakeChampion/db625cb7bde5528f1154f88674638d43 to your computer and use it in GitHub Desktop.
replacer function for json.stringify to change circular references
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