JavaScript洗牌算法打乱数组元素顺序

function shuffle(array) {
    let length = array.length;
    let index;
    let temp;
    while (length > 0) {
        index = Math.floor(Math.random() * length);
        temp = array[length - 1];
        array[length - 1] = array[index];
        array[index] = temp;
        length--;
    }
    return array
}

你可能感兴趣的:(javascript,算法,前端)