小程序修改data中数组或者对象里面的某一项值

核心要点:将需要修改的全局data用字符串的形式定义出来,然后在setData中用中括号包起来

 

js:

 /**
   * 页面的初始数据
   */
  data: {
  
    wage_data: [{
      created_at: "2020-05-15 16:36:01",
      has_like: true,
      id: 2,
      like_num: 1,
      post_class_name: "自我提升",
      profile: "概要",
      title: "主题",
      user_name: "test",
    },
    {
      created_at: "2020-05-15 16:36:01",
      has_like: true,
      id: 3,
      like_num: 1,
      post_class_name: "自我提升",
      profile: "概要",
      title: "主题",
      user_name: "test",
    },{
        created_at: "2020-05-15 16:36:01",
        has_like: true,
        id: 4,
        like_num: 1,
        post_class_name: "自我提升",
        profile: "概要",
        title: "主题",
        user_name: "test",
    }
    ],
  
  },

wxml:

   <block  wx:for="{
    
    {wage_data}}">
          <button bindtap="changData" data-index="{
    
    {index}}">
           </button>
   </block>

js:

​changData: function(e) {
    var index = e.target.dataset.index;
  
    var value = 'wage_data[' + index + '].has_like'+ '';
    this.setData({
      [value]: !this.data.wage_data[index].has_like
    })

​}

猜你喜欢

转载自blog.csdn.net/weixin_40762926/article/details/106146206