微信小程序操作数组对象

navinfo: [{ ‘NavName‘: ‘定位‘, ‘NavIcon‘: ‘location‘, ‘NavIconSize‘: ‘22px‘, ‘IconColor‘: ‘#000‘, ‘IconCheckedColor‘: ‘#fff‘, ‘IsChecked‘: ‘0‘ },
{ ‘NavName‘: ‘电池‘, ‘NavIcon‘: ‘shopping-cart‘, ‘NavIconSize‘: ‘22px‘, ‘IconColor‘: ‘#000‘, ‘IconCheckedColor‘: ‘#fff‘, ‘IsChecked‘: ‘0‘ },
{ ‘NavName‘: ‘救援‘, ‘NavIcon‘: ‘phone-circle‘, ‘NavIconSize‘: ‘22px‘, ‘IconColor‘: ‘#000‘, ‘IconCheckedColor‘: ‘#fff‘, ‘IsChecked‘: ‘1‘ },
{ ‘NavName‘: ‘我的‘, ‘NavIcon‘: ‘manager‘, ‘NavIconSize‘: ‘22px‘, ‘IconColor‘: ‘#000‘, ‘IconCheckedColor‘: ‘#fff‘, ‘IsChecked‘: ‘0‘ }]
如上代码,需要修改navinfo数组里面的第一个对象{ ‘NavName‘: ‘定位‘, ‘NavIcon‘: ‘location‘, ‘NavIconSize‘: ‘22px‘, ‘IconColor‘: ‘#000‘, ‘IconCheckedColor‘: ‘#fff‘, ‘IsChecked‘: ‘0‘ },里的IsChecked为1,其他的子项的IsChecked属性修改为0,
代码:
 _onChange:function(e){
      
      let that=this;
      var _index=e.currentTarget.dataset.index;
      for (let nav in that.properties.NavInfo){
        if (nav == _index){
          that.setData({
            [‘NavInfo[‘ + nav + ‘].IsChecked‘]:1
          });
        }else{
          that.setData({
            [‘NavInfo[‘ + nav + ‘].IsChecked‘]: 0
          });
        }
      }
    }

以上函数中的e,由前端的data-index中获取,页面代码如下:

<view wx:for="{{NavInfo}}" wx:for-item="Item" style="">wx:key="*this" data-index="{{index}}" bindtap="_onChange" class="navItem">

微信小程序操作数组对象

上一篇:微信小程序


下一篇:小程序常用操作,if,for,跳转,弹出提示