wx小程序 更新数组中的对象这样类型的写法

原始数据

menu=[
 {id: "13", price: "22", title: "南洋鸡肉卷套餐", sales: "36", img: "13.jpg",num:0},
 {id: "14", price: "23", title: "菠萝鸡腿堡套餐", sales: "38", img: "14.jpg", num:0},
 {id: "15", price: "29", title: "双鸡堡套餐", sales: "33", img: "15.jpg",num:0}
 ]

更新数据写法

addNum:function(e){
      let id=e.currentTarget.dataset.id;
      let num=1;
    this.data.menu.forEach((item,index)=>{
        if(item.id==id){
          console.log(item.num)
          let n=+item.num+num;
          if(n>=0){
            this.setData({
              ['menu['+index+'].num']:n
            })
          }
        }
      })
      }

 

posted @ 2021-05-08 15:37  还有什么值得拥有  阅读(109)  评论(0编辑  收藏  举报