Я работаю с angular, я получаю данные от api get call для одного из столбцов, как показано ниже. Я преобразовал этот json в массив, как уже упоминалось. И когда я повторяю это по html, я получаю ошибку. Моя цель - перебрать данные столбца ниже в html с помощью ngfor. Пожалуйста, помогите мне достичь функциональности.
[{"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.info(this.data)
this.data.forEach((item,index)=>{
var obj;
obj = {
Parent:item.Parent,
text:item.BrandName,
}
this.data1.push(obj)
});
console.info(JSON.stringify(this.data1))
}
}



Вы не должны помещать массив в объект data.
Вы можете назначить или передать элементы из массива объекту data.
Итак, вы можете сделать это:
this.data = [
{
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
}
];
Или это:
[
{
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
}
];
[
{
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
}
].forEach(f => this.data.push(f));
Примечание: Здесь лучше попробовать первый.
Рабочая демонстрация при StackBlitz.
попробуй это:
Array.prototype.forEach.call(this.data, item => {
//logic goes here...
})