*随机排列数组
1、sort (最简单)
function sort(){
const arr = [0,1,2,3,4,5,6,7,8,9];
//sort中的函数会返回三个值,大于0,等于0,小于0,从而决定a和b的排序位置
return arr.sort((a,b)=>Math.random()-0.5)
}
2、随机取值 (理论上有可能死循环,性能最差)
function sort(){
const arr = [0,1,2,3,4,5,6,7,8,9];
const newArr = [];
const newMap = {};
for(let i=0;i0){
let index = Math.random()*arr.length | 0;
newArr.push(arr[index]);
arr.splice(index,1);
}
return newArr;
}