收藏
回答

JS文件中调用if语句,出现部分跳过执行的情况如何处理?

WXML中很简单

<view class="COptAdv_rowwx:for="{{COptAdv_List}}">

  <view class="COptAdv_Item1">{{item.Adv_Item1}}</view>

  <view class="COptAdv_Dia1">{{item.Adv_Dia_Para1}}</view>

  <view class="COptAdv_Item2">{{item.Adv_Item2}}</view>

  <view class="COptAdv_Dia2">{{item.Adv_Dia_Para2}}</view>

</view>

JS中数据定义如下:

data: {

    COptAdv_List:[

      {Adv_Item1:"XM1:", Adv_Dia_Para1:0, Adv_Item2:"XBR1:", Adv_Dia_Para2:0},

      {Adv_Item1:"XM2:", Adv_Dia_Para1:0, Adv_Item2:"XBR2:", Adv_Dia_Para2:0},

      {Adv_Item1:"XM3:", Adv_Dia_Para1:0, Adv_Item2:"XBR3:", Adv_Dia_Para2:0},

      {Adv_Item1:"XM4:", Adv_Dia_Para1:0, Adv_Item2:"XBR4:", Adv_Dia_Para2:0},

      {Adv_Item1:"XM5:", Adv_Dia_Para1:0, Adv_Item2:"XBR5:", Adv_Dia_Para2:0},

      {Adv_Item1:"XM6:", Adv_Dia_Para1:0, Adv_Item2:"XBR6:", Adv_Dia_Para2:0}

    ],

    XM_P:0,

    XBR_P:0,

  },

Onload里对其进行判断和赋值,程序写得冗长,的确没找到更好的判断方式

onLoad: function (options) {

    var i = 0

    var length = this.data.COptAdv_List.length

    for (let i = 0; i < length; i++{

      console.log(app.Input_Current1)

      this.setData({

        XM_P:app.Input_Current1[i]/app.Input_Parameter.Temp_Para/app.Input_Parameter.Density_Para,

        XBR_P:app.Input_Current1[i+6]/app.Input_Parameter.Temp_Para/app.Input_Parameter.Density_Para,

      })

      console.log(i)

      console.log(this.data.XM_P)

      console.log(this.data.XBR_P)

      // 动力线选线径

      if (this.data.XM_P>=0 && this.data.XM_P<=6{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:0.25,

        })

      }

      if (this.data.XM_P>6 && this.data.XM_P<=7{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:0.35,

        })

      }

      if (this.data.XM_P>7 && this.data.XM_P<=9{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:0.5,

        })

      }

      if (this.data.XM_P>9 && this.data.XM_P<=12{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:0.75,

        })

      }

      if (this.data.XM_P>12 && this.data.XM_P<=15{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:1.0,

        })

      }

      if (this.data.XM_P>15 && this.data.XM_P<=18{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:1.5,

        })

      }

      if (this.data.XM_P>18 && this.data.XM_P<=26{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:2.5,

        })

      }

      if (this.data.XM_P>26 && this.data.XM_P<=34{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:4.0,

        })

      }

      if (this.data.XM_P>34 && this.data.XM_P<=44{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:6.0,

        })

      }

      if (this.data.XM_P>44 && this.data.XM_P<=61{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:10.0,

        })

      }

      if (this.data.XM_P>61 && this.data.XM_P<=82{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:16.0,

        })

      }

      if (this.data.XM_P>82 && this.data.XM_P<=108{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:25.0,

        })

      }

      if (this.data.XM_P>108 && this.data.XM_P<=135{

        this.setData({

          [`COptAdv_List[${i}].Adv_Dia_Para1`]:35.0,

        })

      }

    }

    // 抱闸线选线径

    if (this.data.XBR_P>=0 && this.data.XBR_P<=6{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:0.25,

      })

    }

    if (this.data.XBR_P>6 && this.data.XBR_P<=7{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:0.35,

      })

    }

    if (this.data.XBR_P>7 && this.data.XBR_P<=9{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:0.5,

      })

    }

    if (this.data.XBR_P>9 && this.data.XBR_P<=12{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:0.75,

      })

    }

    if (this.data.XBR_P>12 && this.data.XBR_P<=15{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:1.0,

      })

    }

    if (this.data.XBR_P>15 && this.data.XBR_P<=18{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:1.5,

      })

    }

    if (this.data.XBR_P>18 && this.data.XBR_P<=26{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:2.5,

      })

    }

    if (this.data.XBR_P>26 && this.data.XBR_P<=34{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:4.0,

      })

    }

    if (this.data.XBR_P>34 && this.data.XBR_P<=44{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:6.0,

      })

    }

    if (this.data.XBR_P>44 && this.data.XBR_P<=61{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:10.0,

      })

    }

    if (this.data.XBR_P>61 && this.data.XBR_P<=82{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:16.0,

      })

    }

    if (this.data.XBR_P>82 && this.data.XBR_P<=108{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:25.0,

      })

    }

    if (this.data.XBR_P>108 && this.data.XBR_P<=135{

      this.setData({

        [`COptAdv_List[${i}].Adv_Dia_Para2`]:35.0,

      })

    }

  },

监控控制台,从数据提取到数据计算都没有问题

但是if判断的时候就有问题了,数组中第2列比较和赋值都正常;第4列中,1个正常,5个不正常,求大神指点这种情况怎么处理?

回答关注问题邀请回答
收藏

3 个回答

  • Raul🇨🇳
    Raul🇨🇳
    2022-05-13

    我找到问题所在了,谢谢各位,大括号有一个错位了

    2022-05-13
    有用 1
    回复
  • 武曲心
    武曲心
    2022-05-13

    这一排if。。。你应该搞个中间变量,循环结束后才setData,这么弄很影响性能的,不要每次循环都来一次setData

    2022-05-13
    有用
    回复
  • 微盟
    微盟
    2022-05-12

    this.setData()是异步方法,如果要修改数据以后用数据做判断的话,要等数据修改成功以后,将判断语句写在setData的回调函数中:

    this.setDate({
          XM_P:10,
          XBR_P:12,
    },()=>{
        console.log(XM_P);
        console.log(XBR_P);
        if(XM_P){
    //判断后的执行语句
        } 
    })
    
    2022-05-12
    有用
    回复
登录 后发表内容