Home > Web Front-end > JS Tutorial > body text

Detailed explanation of js disrupting the order of arrays

小云云
Release: 2018-03-10 15:16:05
Original
2550 people have browsed it

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);
Copy after login
//方法二: 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()
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template