blog
blog copied to clipboard
我怎样才能真正洗牌一个数组?
function shuffle(oldArray) {
const array = [...oldArray];
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}