找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 js教程 查看内容

js如何对乱序数组进行排序

作者:模板之家 2020-5-14 09:18 2534人关注

本文为大家介绍了js对乱序数组进行重新排序的方法,具有一定的参考价值,希望可以帮助到大家。对乱序数组进行重新排序的代码是:【arr.sort(function (a, b) {return Math.random()-0.5})】

具体代码为:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        arr.sort(function (a, b) {
            return Math.random() - 0.5; //使用随机数,大于0.5的往后排,小于0.5的往前排
        })
        console.log(arr);
    </script>
</body>

</html>

index.html

路过

雷人

握手

鲜花

鸡蛋
原作者: php中文网 来自: php中文网

全部回复(0)