问TypeError: JSON.stringify无法序列化循环结构ENvarcircularReference={otherData:123};circularReference.m...
JSON.stringify(circularReference);// 报错信息如下VM685:1Uncaught TypeError:Converting circular structure toJSON-->starting at objectwithconstructor'Object'---property'myself'closes the circle atJSON.stringify()at:1:6 可以看到和我上面说的Vue的例子是类似的。 那如何解决呢? 既然是由于循环引用导致的,...
stringify(circularReference); 要序列化循环引用,你可以使用支持循环引用的库(例如 Douglas Crockford 的 cycle.js),或者自己实现一个解决方案,这需要找到循环引用,并用可序列化的值替换(或移除)它们。 如果你在使用 JSON.stringify() 来深拷贝一个对象,你可能想要使用 structuredClone(),它支持循环引用。JavaScript...
解决方案 方案1.使用 JSON.stringify() 的 replacer 参数 JSON.stringify() 第二个可选参数 replacer 可以设置为一个函数或数组。我们可以选择设置为一个函数,在其中过滤掉循环引用(这样会造成数据丢失):在上述代码的 getCircularReplacer 函数中,我们借助 ES6 的 WeakSet 实现了对象属性值的滤重,从而避免了循...
circular-reference In Jade - var cache = []; script var table = !{JSON.stringify(table,function(key, value) {if (typeof value === 'object' && value !== null) {if (cache.indexOf(value) !== -1) {console.log('Found Circular reference: '+key);return;}cache.push(value);}return...
return 'Circular Reference Detected!'; } return value; }; const jsonString = JSON.stringify(circularObj, keyFilter); console.log(jsonString); 在这个例子中,我们定义了一个包含循环引用的对象 circularObj,并使用 keyFilter 函数来检测并替换循环引用属性。这样就可以避免循环引用问题导致的 JSON 序列化失败...
UseJSON.stringifywith a custom replacer. For example: // Demo: Circular reference var o = {}; o.o = o; var cache = []; JSON.stringify(o, function(key, value) { if (typeof value === 'object' && value !== null) { if (cache.indexOf(value) !== -1) { ...
自己实现 JSON.stringify 理解功能的最好方法是自己去实现它。 下面我写了一个简单的函数来模拟JSON.stringify。 复制 constjsonstringify=(data)=>{// Check if an object has a circular referenceconstisCyclic=(obj)=>{// Use the Set data type to store detected objectsletstackSet=newSet()letdetected...
// Demo: Circular referencevaro = {}; o.o= o;// Note: cache should not be re-used by repeated calls to JSON.stringify.varcache = [];JSON.stringify(o,function(key, value) {if(typeofvalue ==='object'&& value !==null) {if(cache.indexOf(value) !== -1) {// Circular reference...
// Demo: Circular referencevaro = {}; o.o= o;// Note: cache should not be re-used by repeated calls to JSON.stringify.varcache = [];JSON.stringify(o,function(key, value) {if(typeofvalue ==='object'&& value !==null) {if(cache.indexOf(value) !== -1) {// Circular reference...