我正在嘗試創建一個名為“reportVersion”的模型
這包含一個陣列 (ReportCategory),其中包含物件集合和兩個變數,一個是數字,另一個是字串。
我創建這個模型只是為了作為引數體傳遞給 API。
由于我沒有在提交表單中創建模型,因此需要將與 like 格式相同的格式作為引數傳遞給 API。我需要從表單模型中獲取值并將其推送到reportVersion。此外,我會將 reportVersion 作為引數傳遞給 API 以呼叫 api。
目前,我在構建 reportVersion 時遇到問題。我已將下面的代碼放在 stackblitz 中。
我不明白為什么 console.log(model) 沒有列印到控制臺。請幫忙。
this.reportCategory.push({ categoryName: 'demo1', weightage: 10 });
this.reportCategory.push({ categoryName: 'demo2', weightage: 20 });
this.reportCategory.push({ categoryName: 'demo3', weightage: 30 });
this.reportCategory.forEach((element) => {
this.reportVersion['categories'].push({
categoryName: element.categoryName,
weightage: element.weightage,
});
});
console.log('this is final output');
console.log(this.reportCategory);
https://stackblitz.com/edit/angular-ivy-vu99yk?file=src/app/hello.component.ts
uj5u.com熱心網友回復:
您需要先初始化reportCategory,然后再對其使用方法(如推送)。
this.reportCategory = []; // Added this line
this.reportCategory.push({ categoryName: 'demo1', weightage: 10 });
this.reportCategory.push({ categoryName: 'demo2', weightage: 20 });
this.reportCategory.push({ categoryName: 'demo3', weightage: 30 });
它在我身邊的 StackBlitz 中作業:)
// 迪倫
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/446724.html
標籤:javascript 有角度的 多维数组 角8
下一篇:遍歷物件陣列中的嵌套物件陣列
