Converting circular structure to JSON\n --> starting at object with constructor 'TLSSocket'\n | property 'parser' -> object with constructor 'HTTPParser'\n --- property 'socket' closes the circle code example
Example 1: json stringify close circle
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
JSON.stringify(circularReference, getCircularReplacer());
Example 2: Converting circular structure to JSON
var a = {};
a.b = a;