Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save plugn/b8de91c4d8401be19c2e58d579db0f9b to your computer and use it in GitHub Desktop.
Save plugn/b8de91c4d8401be19c2e58d579db0f9b to your computer and use it in GitHub Desktop.
JSON.stringify() - debug TypeError: Converting circular structure to JSON
'use strict';
function replacer() {
var objects = [];
return function(key, value) {
if (typeof value === 'object' && value !== null) {
var found = objects.some(function(existing) {
return (existing === value);
});
if (found) {
return '[Circular: ' + key + ']';
}
objects.push(value);
}
return value;
};
}
var b = {};
var a = {
x: {
y: {
z: b
}
}
};
b.a = a;
console.log(JSON.stringify(a, replacer(), ' '));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment