+
95
-

js如何随机打乱数组?

请问js如何随机打乱数组?

网友回复

+
15
-

要随机打乱 JavaScript 数组,可以使用 Fisher-Yates 随机算法或现有的现成方法。以下是两种常见的方法:

1. Fisher-Yates 随机算法

Fisher-Yates 算法是一种用于将数组随机打乱的有效算法,步骤如下:

function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array...

点击查看剩余70%

我知道答案,我要回答