json循环引用报错解决方案

export function customStringify(v) {
  const cache = new Set();
  return JSON.stringify(v, function(key, value) {
    if (typeof value === 'object' && value !== null) {
      if (cache.has(value)) {
        // Circular reference found
        try {
          // If this value does not reference a parent it can be deduped
          return JSON.parse(JSON.stringify(value));
        } catch (err) {
          // discard key if value cannot be deduped
          return;
        }
      }
      // Store value in our set
      cache.add(value);
    }
    return value;
  });
}

  

posted @ 2020-10-16 15:28  你丫才美工  阅读(307)  评论(0编辑  收藏  举报