美文网首页
小程序中使用setData改变动态数组的对象值

小程序中使用setData改变动态数组的对象值

作者: 九旬大爷的梦 | 来源:发表于2019-09-25 18:24 被阅读0次

    js函数代码

    add:function(e){
        let id = e.currentTarget.dataset.id // 获取传入的id
        let index = e.currentTarget.dataset.index  // 获取数据的索引
        let temp = 'goodsList[' + index +'].num'  // 获取goodsList[index].num **这是重点**
        this.setData({
          [temp]:this.data.goodsList[index].num + 1
        })
      }
    let temp = 'goodsList[' + index +'].num'为主要代码,打印出的数据为goodsList[index].num
    

    注:index为变量,根据传入的索引值而改变

    相关文章

      网友评论

          本文标题:小程序中使用setData改变动态数组的对象值

          本文链接:https://www.haomeiwen.com/subject/ovzluctx.html