typescript for exception

...

for 循环之前需要初始化被赋值的对象,否则每次被赋值的是同一个对象,而且是这个对象最后一次被赋值的情况

参考 https://blog.csdn.net/qq_38758765/article/details/115749097

HuDerr(): void{
    this.data3.data.sections.forEach(
      (value, index, array) =>{
        console.log('L1', value);
        let va = array[index];
        this.IIdd.title = va.name;
        this.IIdd.id = va.sectionId;
        va.items.forEach(
          (value2, index1, array1) => {
            console.log('L2 for ====>', value2, 'index1==>' ,index1, 'arr1==>', array1);
            let vaa = array1[index1];
            this.Idd.title = vaa.name;
            this.Idd.id = vaa.id;
          }
        );
        this.IIdd.children.push(this.Idd);
        console.log('L1===>', this.IIdd);
        console.log('l1 sep===>');
        this.data1.push(this.IIdd);
        // console.log('in for data1===>', this.data1);
        return;
      }
    );
  }

  HuD(): void {

    for (const item of this.data3.data.sections) {
      this.IIdd = {
        id: '',
        title: '',
        children: []
      };
      this.IIdd.title = item.name;
      this.IIdd.id = item.sectionId;
      console.log(this.IIdd);
      const va = item;
      for (let emm of va.items){
        this.Idd = {id: '', title: '' };
        const mm =emm;
        console.log('emm===>', mm);
        this.Idd.id=mm.id;
        this.Idd.title=mm.name;
        this.IIdd.children.push(this.Idd);
        console.log(this.IIdd);
        continue;
      }
      this.data1.push(this.IIdd);
      console.log(this.data1);
      console.log('L1 spw');
    }
  }

  

原文地址:https://www.cnblogs.com/eiguleo/p/14797818.html