微信小程序 setData 只修改对象中的一个值,其他值保持不变

微信小程序  setData  只修改对象中的一个值,其他值保持不变

对象数据:

  data: {
    userinfo: {
      id: 1,
      nickname: "xxx",
      phone: "xxxxx",
      sex: "男",
      age: 0,
      avatar: "xxxx",
    }
  },

这种方法,会让丢失对象中的其他属性,代码如下:

 this.setData({
          userinfo:{
            avatar: xxx
          }        
        })
//最后userinfo中只有avatar属性了,其他值没了

使用下面这种方法即可解决:

 this.setData({
          ['userinfo.avatar']: aaaa
        })

这样就达到,只修改其中一个值,其他值保持不变,

若想修改多个值,形式类似,如:

  this.setData({
          ['userinfo.avatar']: aaa,
          ['userinfo.age']: bbbb,
          ['userinfo.nickname']:ccccc,
        })
原文地址:https://www.cnblogs.com/zqblog1314/p/12831488.html