我试图遍历项目数组并推送到另一个数组,但项目列表中的索引为“ 0”,但仍然能够获得所提及的错误。 请找到以下代码
createReportFormulaInfo(): Array<ReportFormulaEntity> {
debugger;
for (let sta in this.selectedSources) {
debugger;
let ido = this.selectedSource[sta].id;
this.selectedFormulaList.push({
Market_OV: this.selectedSources[sta].Market,
Set_Name: this.selectedSources[sta].Value,
Data_ID: Number(this.selectedSources[sta].Market),
Set_number: 0,
Formula_Set1: "",
Formula_Set2: "",
Formula_type: "",
Date_From: "",
Date_To: "",
UpdateFlag: "A",
Is_Current: 0
});
}
}
我不确定如何解决此问题,我正在使用角度4。 任何人都可以帮助这个查询。 预先感谢。
答案 0 :(得分:1)
注意:for ... in不应用于遍历其中 索引顺序很重要。
您将基于局部变量进行迭代
createReportFormulaInfo(): Array<ReportFormulaEntity> {
debugger;
for (i=0;i<this.selectedSources.length;i++) {
debugger;
let ido = this.selectedSource[i].id;
this.selectedFormulaList.push({
Market_OV: this.selectedSources[i].Market,
Set_Name: this.selectedSources[i].Value,
Data_ID: Number(this.selectedSources[i].Market),
Set_number: 0,
Formula_Set1: "",
Formula_Set2: "",
Formula_type: "",
Date_From: "",
Date_To: "",
UpdateFlag: "A",
Is_Current: 0
});
}
}
另一种方法是使用数组的foreach方法
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach