打乱给定数组,返回新数组,不修改原数组
function shuffle(arr) {
let
n = arr.length,
newArr = [...arr]
while (n) {
let m = Math.floor(Math.random() * n-- );
[newArr[m], newArr[n]] = [newArr[n], newArr[m]];
}
return newArr
}
打乱给定数组,返回新数组,不修改原数组
function shuffle(arr) {
let
n = arr.length,
newArr = [...arr]
while (n) {
let m = Math.floor(Math.random() * n-- );
[newArr[m], newArr[n]] = [newArr[n], newArr[m]];
}
return newArr
}
本文标题:数组打乱的代码片段
本文链接:https://www.haomeiwen.com/subject/mwiplxtx.html
网友评论