Home  >  Article  >  Web Front-end  >  Detailed explanation of js disrupting the order of arrays

Detailed explanation of js disrupting the order of arrays

小云云
小云云Original
2018-03-10 15:16:052560browse

This article mainly shares with you the detailed explanation of js disrupting the order of arrays. There are two methods. I hope it can help everyone.

    //方法一:也是最简单的方法
    var arr=[];    for(var i=0;i<100;i++){
            arr[i]=i;
        }
    arr.sort(function(){ return 0.5 - Math.random() })    var str=arr.join();
    alert(str);
//方法二: Fisher–Yates洗牌算法var arr = new Array(1,2,3,5);Array.prototype.shuffle = function() {
    var array = this;    var m = array.length,
        t, i;    while (m) {
        i = Math.floor(Math.random() * m--);
        t = array[m];        array[m] = array[i];        array[i] = t;
    }    return array;
}
arr.shuffle()

Related recommendations:

Disordering the order of numbers and associative arrays in php_PHP tutorial

The above is the detailed content of Detailed explanation of js disrupting the order of arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn