zoukankan      html  css  js  c++  java
  • js去重复和取重复数据

    js数组中取重复数据的方法:
    方法一:去重复数据
    <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=['a','b','c','d','b','a','e','a','b','c','d','b','a','e'];
    document.write('原始数组:'+x);
    document.write("<br />");
    document.write('去重复后:'+x.distinct());
    </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=['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.distinct());
    </script>
  • 相关阅读:
    html部分常用内容
    Django media相关配置
    【设计模式】-单例模式
    SharePoint 修改完或制作完一定要发布
    SharePoint 创建模版页
    kindeditor 不能编辑 问题
    1
    SharePoint 第一个网站
    数据结构第一章
    未能加载文件或程序集“MICROSOFT.REPORTVIEWER.WEBFORMS …
  • 原文地址:https://www.cnblogs.com/manwwx129/p/7245298.html
Copyright © 2011-2022 走看看