js中将数组中元素随机排列
作者:
一粒沙随风飘摇 | 来源:发表于
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
})
},
本文标题:js中将数组中元素随机排列
本文链接:https://www.haomeiwen.com/subject/cbwdeftx.html
网友评论