微信小程序:for..in裏用if..else致使不能for循環的問題

圖片描述
請求接口,得到一個對象數組,將res.data裏的數據push到result數組裏圖片描述
有四種狀態,根據狀態type不一樣來判斷須要顯示的數據,可是我將if判斷寫在了for..in裏就會出現數據不循環的狀況,全部我用了forEach()的方法,forEach() 方法用於調用數組的每一個元素,並將元素傳遞給回調函數,在回調函數裏進行判斷,將判斷的屬性和屬性值寫進對象裏,而後在wxml裏經過wx:if=''來進行判斷。圖片描述
圖片描述數組

js:
page({
    result:[],
    cancleBtn :true,//默認爲true
    get(){
        utils.askData('get', url, '', (res) => {//請求接口,res.data是個數組,將res.data裏的數據push到result裏
          for (let key in res.data) {
                result.push(res.data[key]);
            //if(res.data[key].type == "1"){
            //this.setData({
             //   cancleBtn :'true',
           // })
           // }else{
             //this.setData({
             //   cancleBtn :'false',
           // })
          // }
          }
          //能夠循環
        result.forEach(function(v){//將是否顯示寫入result數組裏
        if(v.type=="1")
        { v.cancleBtn = true;}
        if (v.type== "3"){
          v.cancleBtn = false;
        }
        }, '');
    }
    });

wxml:
<view wx:for="{{result}}" wx:key="{{index}}">
    <view wx:if="{{item.cancleBtn}}"></view>
</view>
相關文章
相關標籤/搜索