打乱数组内数据顺序

从数组最后一个数据开始和它前面的任意索引对应的数据交换

let datalist = [1, 2, 3, 4, 5, "a", "b", "c", 6, 7, 8, 9, 12, 14, 15];

function shuffle(arr) {
let m = arr.length;
while(m > 1) {
let index = Math.floor(Math.random() * m--);//交换的索引
[arr[m], arr[index]] = [arr[index], arr[m]] //交换数据
}
return arr;
}
var data = shuffle(datalist)
console.log(data)

猜你喜欢

转载自www.cnblogs.com/youngMe/p/11022078.html