从数组最后一个数据开始和它前面的任意索引对应的数据交换
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)
原文地址:https://www.cnblogs.com/youngMe/p/11022078.html
时间: 2024-11-02 01:52:26