打字稿对象动态分配更改fieldName

时间:2019-05-12 18:35:41

标签: angular typescript ionic3

我正在使用现有对象中的某些值创建一个新对象。现在,在为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。

1 个答案:

答案 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);