-
-
Save plugn/b8de91c4d8401be19c2e58d579db0f9b to your computer and use it in GitHub Desktop.
JSON.stringify() - debug TypeError: 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
'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