js中将数组中元素随机排列

2018-06-13  本文已影响0人  一粒沙随风飘摇
switch: function (e) {
     //数组的长度
    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
    })
  },
上一篇 下一篇

猜你喜欢

热点阅读