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

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
    })
  },

推荐阅读更多精彩内容