I am going to convert a array into a json string. However, when I convert it into json, a circular reference problem has been thrown. And the array is too complex that I can't find out the circular reference manually.
And I have tried the following function but it does not work
function isCyclic (obj) {
var seenObjects = [];
function detect (obj) {
if (typeof obj === 'object') {
if (seenObjects.indexOf(obj) !== -1) {
return true;
}
seenObjects.push(obj);
for (var key in obj) {
if (obj.hasOwnProperty(key) && detect(obj[key])) {
console.log(obj, 'cycle at ' + key);
return true;
}
}
}
return false;
}
return detect(obj);
}
Does anyone has any other solution to find out the circular reference? Thanks~