JSON的循环结构,UI运行时错误TypeError
我在尝试解析和字符串化一些JSON数据时出错 在这一行:JSON的循环结构,UI运行时错误TypeError,json,angular,typescript,Json,Angular,Typescript,我在尝试解析和字符串化一些JSON数据时出错 在这一行: this.copyOfColumns = JSON.parse(JSON.stringify(Object.assign([], this.columns))); 以下是整个@输入(使用角度4): @Input() 设置gridColumns(GridColumnSAR:Array){ log('gridcolumnsar'); console.log(gridcolumnsar); this.columns=this.sortActi
this.copyOfColumns = JSON.parse(JSON.stringify(Object.assign([], this.columns)));
以下是整个@输入(使用角度4):
@Input()
设置gridColumns(GridColumnSAR:Array){
log('gridcolumnsar');
console.log(gridcolumnsar);
this.columns=this.sortActiveAndInactiveColumns(gridcolumnsar);
console.log('this.columns');
console.log(this.columns);
this.copyOfColumns=JSON.parse(JSON.stringify(Object.assign([],this.columns));
console.log('this.copyOfColumns');
console.log(this.copyOfColumns);
}
以下是记录到控制台(this.columns)的数据以及以下错误:
我假设您希望通过使用
JSON.parse(JSON.stringify())
对数组进行深度复制。显然,您的数据结构中有一个错误,它使JSON.stringify()
失败
您应该清理数据,使其不包含ciruclar引用,或者可以尝试使用类似的库。为什么不设置this.copyOfColumns=JSON.stringify(Object.assign([],this.columns));尝试了之后,得到了完全相同的错误
@Input()
set gridColumns(gridColumnsArr: Array<object>) {
console.log('gridColumnsArr');
console.log(gridColumnsArr);
this.columns = this.sortActiveAndInactiveColumns(gridColumnsArr);
console.log('this.columns');
console.log(this.columns);
this.copyOfColumns = JSON.parse(JSON.stringify(Object.assign([], this.columns)));
console.log('this.copyOfColumns');
console.log(this.copyOfColumns);
}