Skip to content

Instantly share code, notes, and snippets.

@zedar
Last active August 29, 2015 14:00
Show Gist options
  • Save zedar/11078029 to your computer and use it in GitHub Desktop.
Save zedar/11078029 to your computer and use it in GitHub Desktop.
JSON.stringify - workaround for "converting circular structure to JSON"
// Assume obj is an object with circular structure
var cache = [];
var str = JSON.stringify(obj, function(key, value) {
if (typeof value === "object" && value !== null) {
if (cache.indexOf(value) !== -1) {
return;
}
cache.push(value);
}
return value;
});
console.log(str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment