美文网首页
JS随机排列数组中的元素

JS随机排列数组中的元素

作者: chiugi | 来源:发表于2019-12-20 14:56 被阅读0次

    如何随机排列数组中的元素
    要用到Array中万能的sort()方法

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

    相关文章

      网友评论

          本文标题:JS随机排列数组中的元素

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