无为清净楼资源网 Design By www.qnjia.com
方法一:去重复数据
复制代码 代码如下:
<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>
标签:
JS数组去重

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com