列表渲染
Page({
data: {
objectArray: [
{ id: 5, un: 'un_5' },
{ id: 4, un: 'un_4' },
{ id: 3, un: 'un_3' },
{ id: 2, un: 'un_2' },
{ id: 1, un: 'un_1' },
{ id: 0, un: 'un_0' }, //定义按钮数组
],
numberArray: [4,3,2,1] //简单定义
},
switch: function () {
const length = this.data.objectArray.length //长度
for (let i = 0; i < length; ++i) {
const x = Math.floor(Math.random() * length) //生成随机数
const y = Math.floor(Math.random() * length)
const temp = this.data.objectArray[x] //数组内容的互换,循环六次,实现打乱
this.data.objectArray[x] = this.data.objectArray[y]
this.data.objectArray[y] = temp
}
this.setData({ //修改数据,修改指定的对象的数据,在这里是修改的第一个数组的数据
objectArray: this.data.objectArray
})
},
addToFront: function () { //添加数据,添加按钮 .concat 连接符,用于连接两个数组
const length = this.data.objectArray.length
this.data.objectArray = [{ id: length, unique: 'un_' + length }].concat(this.data.objectArray)
this.setData({ //数据的添加的改变,下同
objectArray: this.data.objectArray
})
},
addNumberToFront: function () {
this.data.numberArray = [this.data.numberArray.length + 1].concat(this.data.numberArray)
this.setData({
numberArray: this.data.numberArray
})
}
})