for (var i = 0; i that.data.IPS.length; i++) {if (that.data.IPS[i].IP_RECID == e.target.dataset.ip) {var DZZT = "IPS[" + i + "].DZZT"var DZSL = "IPS[" + i + "].DZSL"if (that.data.IPS[i].DZZT == 1) {that.setData({[DZZT]: 0,[DZSL]: that.data.IPS[i].DZSL - 1})} else {that.setData({[DZZT]: 1,[DZSL]: that.data.IPS[i].DZSL + 1})}break;}}

代码说明:此功能实现微信小程序点赞功能,即无需重新请求列表数据,即可实现点赞数量和状态的变化。

IPS是个数组,根据IPS进行循环,数组中包括了主键,点赞数量和状态。

此代码的关键部分是:var DZZT = "IPS[" + i + "].DZZT",然后通过

that.setData({[DZZT]: 0,[DZSL]: that.data.IPS[i].DZSL - 1})这种方式进行赋值。

有问题可留言交流。

TAG标签: 微信小程序遍历对象

http://www.xiaochengxub.com/read-105632.html