Last active
August 29, 2015 14:00
-
-
Save zedar/11078029 to your computer and use it in GitHub Desktop.
JSON.stringify - workaround for "converting circular structure to JSON"
This file contains 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
// 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