zoukankan      html  css  js  c++  java
  • 36.数组去重

     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8">
     5     <title>数组去重</title>
     6 
     7 </head>
     8 <body>
     9 </body>
    10 <script type="text/javascript">
    11 
    12     var strs = ['a', 'a', 5, 5,7];
    13 
    14     function remove1(str) {
    15         var newStr = [], obj = {};
    16         for (var i = 0, h = str.length; i < h; i++) {
    17             if (!obj[str[i]]) {
    18                 newStr.push(str[i]);
    19                 obj[str[i]] = 1;
    20             }
    21         }
    22         return newStr;
    23     }
    24     var s1 = remove1(strs);
    25     console.log('s1:' + s1);
    26 
    27 
    28     function remove2(str) {
    29         return Array.from(new Set(str));
    30     }
    31 
    32     var s2 = remove2(strs);
    33     console.log('s2:' + s2)
    34 
    35 
    36     function remove3(str) {
    37         var newStr = [];
    38         for (var i = 0, j = str.length; i < j; i++) {
    39             if (newStr.indexOf(str[i]) === -1) {
    40                 newStr.push(str[i]);
    41             }
    42         }
    43         return newStr;
    44     }
    45     var s3=remove3(strs);
    46     console.log('s3:'+s3);
    47 
    48     //4
    49     function remove4(str) {
    50         var newStr = [];
    51 
    52         str.forEach(function(e, i, str) {
    53             if (str.indexOf(e) === i) {
    54                 newStr.push(e);
    55             }
    56         });
    57 
    58         return newStr;
    59     }
    60 
    61     var s4=remove4(strs);
    62     console.log('s4:'+s4);
    63 
    64     //5
    65     function remove5(str) {
    66         var newStr = [],
    67                end;
    68         str.sort();
    69         end = str[0];
    70         newStr.push(str[0]);
    71 
    72         for (var i = 1; i < str.length; i++) {
    73             if (str[i] != end) {
    74                 newStr.push(str[i]);
    75                 end = str[i];
    76             }
    77         }
    78         return newStr;
    79     }
    80 
    81     var s5=remove5(strs);
    82     console.log('s5:'+s5);
    83 
    84 
    85 
    86 </script>
    87 
    88 </html>
    View Code

    效果图:

    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title>数组去重</title>

    </head>
    <body>
    </body>
    <script type="text/javascript">

    var strs = ['a', 'a', 5, 5,7];

    function remove1(str) {
    var newStr = [], obj = {};
    for (var i = 0, h = str.length; i < h; i++) {
    if (!obj[str[i]]) {
    newStr.push(str[i]);
    obj[str[i]] = 1;
    }
    }
    return newStr;
    }
    var s1 = remove1(strs);
    console.log('s1:' + s1);


    function remove2(str) {
    return Array.from(new Set(str));
    }

    var s2 = remove2(strs);
    console.log('s2:' + s2)


    function remove3(str) {
    var newStr = [];
    for (var i = 0, j = str.length; i < j; i++) {
    if (newStr.indexOf(str[i]) === -1) {
    newStr.push(str[i]);
    }
    }
    return newStr;
    }
    var s3=remove3(strs);
    console.log('s3:'+s3);

    //4
    function remove4(str) {
    var newStr = [];

    str.forEach(function(e, i, str) {//固定顺序,e是数组元素,i是数组元素下标
    if (str.indexOf(e) === i) {
    newStr.push(e);
    }
    });

    return newStr;
    }

    var s4=remove4(strs);
    console.log('s4:'+s4);

    //5
    function remove5(str) {
    var newStr = [],
    end;
    str.sort();
    end = str[0];
    newStr.push(str[0]);

    for (var i = 1; i < str.length; i++) {
    if (str[i] != end) {
    newStr.push(str[i]);
    end = str[i];
    }
    }
    return newStr;
    }

    var s5=remove5(strs);
    console.log('s5:'+s5);



    </script>

    </html>
  • 相关阅读:
    CentOS7.4 chrony时间同步服务器部署(替代NTPD)
    Zabbix Agent for Windows部署(五)
    Zabbix3.4.5部署安装(二)
    Zabbix概术及基础介绍(一)
    修改zabbix为中文,并解决乱码问题(三)
    ZABBIX 3.4 监控Nginx 状态(七)
    PXE+Kickstart 全自动安装部署CentOS7.4
    Zabbix Agent for Linux部署(四)
    不能在 DropDownList 中选择多个项 原因分析及解决方法
    IE浏览器下的CSS问题
  • 原文地址:https://www.cnblogs.com/mx2036/p/6945645.html
Copyright © 2011-2022 走看看