const circularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
const clonedObj = JSON.parse(JSON.stringify(obj, circularReplacer()));
const jsonStr = JSON.stringify(clonedObj, null, 2);
const blob = new Blob([jsonStr], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.download = filename;
link.href = url;
link.click();
}
circularReplacer(obj, 'obj.json')
在这之前需要将对象存储为变量,然后在控制台调用此方法即可。