【发布时间】:2021-06-30 11:33:38
【问题描述】:
我正在使用 Angular,我正在从 api 获取数据,获取以下列之一的调用。 如前所述,我将此 json 转换为数组。当我在 html 上迭代它时,我得到了错误。 我的目标是使用 ngfor 在 html 中迭代以下列数据。 请帮我实现功能。
[{"Parent": null, "GradeCode": null, "BrandName": "CORONA", "Manufacturer": null, "Description": null, "BrandStatus": 1, "ManufacturerName": "", "ParentBrandName": "CORONA", "id": 7101},
{"Parent": "hhh", "GradeCode": null, "BrandName": "TSINGTAO", "Manufacturer": null, "Description": null, "BrandStatus": 1, "ManufacturerName": "", "ParentBrandName": "TSINGTAO", "id": 7100}]
我将此数据推送到数组
data: any = [];
我将上面的“数据”从 json 转换为数组,如下所示。 但是,我得到的错误是:
" ERROR TypeError: this.data.forEach 不是函数。
data1=[];
ngOnInit()
{
var StringifyData=JSON.stringify(this.data)
console.log(this.data)
this.data.forEach((item,index)=>{
var obj;
obj={
Parent:item.Parent,
text:item.BrandName,
}
this.data1.push(obj)
});
console.log(JSON.stringify(this.data1))
}
}
【问题讨论】: