Ruby's Louvre

每天学习一点点算法

导航

处理TypeError: Converting circular structure to JSON

// Demo: Circular reference
var o = {};
o.o = o;

// Note: cache should not be re-used by repeated calls to JSON.stringify.
var cache = [];
JSON.stringify(o, function(key, value) {
    if (typeof value === 'object' && value !== null) {
        if (cache.indexOf(value) !== -1) {
            // Circular reference found, discard key
            return;
        }
        // Store value in our collection
        cache.push(value);
    }
    return value;
});
cache = null;


posted on 2017-05-05 18:24  司徒正美  阅读(15618)  评论(1编辑  收藏  举报