美文网首页
js数组乱序

js数组乱序

作者: 浪天林 | 来源:发表于2020-08-06 11:07 被阅读0次

    var values = [1, 2, 3, 4, 5];
    
    values.sort(function(){
        return Math.random() - 0.5;
    });
    
    console.log(values)
    

    var res= [1, 2, 3, 4, 5];
    function shuffle(a) {
        for (let i = a.length; i; i--) {
            let j = Math.floor(Math.random() * i);
            [a[i - 1], a[j]] = [a[j], a[i - 1]];
        }
        return a;
    }
    console.log(shuffle(res))

    相关文章

      网友评论

          本文标题:js数组乱序

          本文链接:https://www.haomeiwen.com/subject/ofewrktx.html