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

96
Waullen
2018.06.13 09:17 字数 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
    })
  },
随笔
Web note ad 1