• JS中取数组中重复的元素和去除重复的元素


    From : http://zhangbq168.blog.163.com/blog/static/23735305200952902915902/

    //去重复数据
    <script>
    Array.prototype.deldistinct=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=['a','b','c','d','b','a','e','a','b','c','d','b','a','e'];
    document.write('原始数组:'+x);
    document.write("<br />");
    document.write('去重复后:'+x.deldistinct());
    </script>

    //取重复数据
    <script type="text/javascript">
    Array.prototype.getdistinct = 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.getdistinct1();
    }

    Array.prototype.getdistinct1 = 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=['a','b','c','d','b','a','e','a','b','c','d','b','a','e','f','f','g'];
    document.write('原始数组:'+x);
    document.write("<br />");
    document.write('去重复后:'+x.getdistinct());
    </script>



  • 相关阅读:
    BeautifulSoup的基本用法
    打印实例
    webservice和一般处理程序
    C# 后台调用存储过程
    表格增加删除
    asp.net C# 获得配置文件AppSettings 的值
    深入浅出zookeeper(一)
    resource下的excel文件下载被损害
    csdn添加目录
    spring面试题,求求你别问我spring了!
  • 原文地址:https://www.cnblogs.com/Athrun/p/2196669.html
Copyright © 2020-2023  润新知