1.一维数组按指定的数量分成二维数组
const pages = [];
this.itemList.forEach((item,index)=>{
const page = Math.floor(index / 10);
if(!pages[page]){
pages[page] = [];
}
pages[page].push(item);
})
2.在不知道obj是否存在的时候的初始化
//页面初始化时
onLoad: function (options) {
var index = 1;
var obj = wx.getStorageSync("data")
if(obj){
if(obj[index]){
this.setData({flag : obj[index]})
}
}else{
obj = {
index : false
}
wx.setStorageSync("data", obj)
}
},
//触发事件时按钮的切换
show(){
var index = 1;
var obj = wx.getStorageSync("data")
obj[index] = !obj[index]
this.setData({ flag: obj[index]})
wx.setStorageSync("data", obj)
},
3.三元表达式的活用
if (obj[index]){
wx.showToast({
title: '添加收藏',
icon: 'success',
duration: 500
})
}else{
wx.showToast({
title: '取消收藏',
icon: 'success',
duration: 1500,
mask:true
})
}
wx.showToast({
title: obj[index] ? "收藏成功" : "取消收藏",
icon: 'success',
duration: 500
})