需要对JSON数据做深度保护,将其中的循环结构进行剥离或者序列化。以下为示例代码:
// 剥离循环结构
const getCircularReplacer = () => { const seen = new WeakSet() return (key, value) => { if (typeof value === "object" && value !== null) { if (seen.has(value)) { return } seen.add(value) } return value } }
const circularObj = { foo: "bar", } circularObj.circularRef = circularObj JSON.stringify(circularObj, getCircularReplacer())
// 序列化循环结构
const obj = { bar: "baz", } obj.obj = obj JSON.stringify(obj, ["bar", "obj"], 2)
上一篇:报错信息“ERRORTypeError:function()isnotafunction”应该怎么理解并解决?
下一篇:报错信息“export'component'(importedas'component')wasnotfoundin'react'(possibleexports:”的中文翻译。