列表渲染

列表渲染

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
    })
  }
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值