微信小程序动态改变数组或对象中的某个属性值

动态改变数组或对象中的属性值

  • wxml
<view wx:for="{{list}}" wx:key="{{item.id}}">
  <view bindtap='select' data-id="{{item.id}}" class="view1 {{item.selected?'active': ''}}">{{item.name}}</view>
</view>
  • js
data:{
 list:[{id: 1, name:'小明', selected:false}{id: 2, name: '小张', selected:false}]
 
 select(e){
   let that = this
   let id = e.currentTarget.dataset.id
   let arr = that.data.list
   arr.forEach((item, index) =>{
     var temp= "list["+ index + "].selected";
     this.setData({
       [temp]: " "
     })
     if(item.id == id) {
	   var temp= "list[" + index + "].selected"
	   this.setData({
	   [temp]: "true"
	   })
    }
 }
}

你可能感兴趣的:(微信小程序,小程序)