• JS数组去重和取重


    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>JS数组去重和取重</title>
    </head>
    
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
    
    <body>
    </body>
    <script>
        Array.prototype.distinct = function () {
            var a = [], b = [];
            for (var prop in this) {
                var d = this[prop];
                if (d === a[prop]) continue; //防止循环到prototype
                if (b[d] != 1) {
                    a.push(d);
                    b[d] = 1;
                }
            }
            return a;
        }
        var x = [1,1,2,2,3,4,4,5];
        document.write('原始数组:' + x);
        document.write("<br />");
        document.write('去重复后:' + x.distinct());
        document.write("<br />");
    </script>
    方法二:取重复数据
    <script type="text/javascript">
        Array.prototype.distinct = function () {
            var a = [], b = [], c = [], d = [];
            for (var prop in this) {
                var d = this[prop];
                if (d === a[prop]) {
                    continue;
                }//防止循环到prototype
                if (b[d] != 1) {
                    a.push(d);
                    b[d] = 1;
                }
                else {
                    c.push(d);
                    d[d] = 1;
                }
            }
    //return a;
            return c.distinct1();
        }
        Array.prototype.distinct1 = function () {
            var a = [], b = [];
            for (var prop in this) {
                var d = this[prop];
                if (d === a[prop]) continue; //防止循环到prototype
                if (b[d] != 1) {
                    a.push(d);
                    b[d] = 1;
                }
            }
            return a;
        }
        var x = [1,1,2,2,3,4,4,5];
        document.write("<br />");
        document.write('原始数组:' + x);
        document.write("<br />");
        document.write('去重复后:' + x.distinct());
    </script>
    </html>
  • 相关阅读:
    模板-树链剖分
    bzoj2523 聪明的学生
    P1220 关路灯
    BZOJ3572 [Hnoi2014]世界树
    BZOJ4013 [HNOI2015]实验比较
    BZOJ4012 [HNOI2015]开店
    BZOJ4011 [HNOI2015]落忆枫音
    BZOJ4009 [HNOI2015]接水果
    BZOJ4010 [HNOI2015]菜肴制作
    BZOJ4008 [HNOI2015]亚瑟王
  • 原文地址:https://www.cnblogs.com/tytsp/p/9687322.html
Copyright © 2020-2023  润新知