Orion's Studio.

随机数组

2024/03/23

写个方法随机打乱一个数组?

1
2
3
4
5
6
7
8
const shuffle = (arr) => {
const result = arr.slice();
for (let i = 0; i < result.length; i++) {
const randomIndex = Math.floor(Math.random() * result.length);
[result[i], result[randomIndex]] = [result[randomIndex], result[i]];
}
return result;
};
CATALOG
  1. 1. 写个方法随机打乱一个数组?