我正在使用现有对象中的某些值创建一个新对象。现在,在为crs.parentFieldId分配fieldId的同时,在最终对象中,fieldId名称本身更改为parentFieldId。这里出了什么问题? 1.代码段
ionViewWillEnter() {
console.log('ionViewWillEnter EditSubfieldFeePage');
this.fs.getCourseFieldBySelectedId().subscribe((cf: CourseField) => {
let ids = this.fs.getSelectedCourses();
for(let crs of cf.courses) {
if(ids.has(crs.courseId)) {
let courseFee: CourseFee = {
fieldId: crs.parentFieldId,
fieldName: cf.fieldName,
courseId: crs.courseId,
courseName: crs.courseName,
feeAmount: 0,
feeType: "MONTHLY"
}
this.courseFeeList.push(crs);
}
}
});
}
类定义
export class CourseFee {
fieldId: string;
courseId: string;
feeAmount: number;
feeType: string;
}
它工作正常,但在控制台中将fieldId重命名为parentFieldId。
答案 0 :(得分:1)
似乎您不是在添加新的courseFee
对象,而是crs
对象(具有parentFieldId
属性)
if(ids.has(crs.courseId)) {
let courseFee: CourseFee = {
fieldId: crs.parentFieldId,
fieldName: cf.fieldName,
courseId: crs.courseId,
courseName: crs.courseName,
feeAmount: 0,
feeType: "MONTHLY"
}
this.courseFeeList.push(crs); // <--- here!
}
要修复此问题,请更改此行
this.courseFeeList.push(crs);
成为
this.courseFeeList.push(courseFee);