欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS數(shù)組去重與取重的示例代碼

 更新時間:2014年01月24日 09:31:35   作者:  
本篇文章主要是對JS數(shù)組去重與取重的示例代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
方法一:去重復數(shù)據(jù)
復制代碼 代碼如下:

<script>
Array.prototype.distinct=function(){
var a=[],b=[];
for(var prop in this){
   var d = this[prop];
   if (d===a[prop]) continue; //防止循環(huán)到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('原始數(shù)組:'+x);
document.write("<br />");
document.write('去重復后:'+x.distinct());
</script>

方法二:取重復數(shù)據(jù)
復制代碼 代碼如下:

<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;
    }//防止循環(huán)到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; //防止循環(huán)到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('原始數(shù)組:'+x);
document.write("<br />");
document.write('去重復后:'+x.distinct());
</script>

相關文章

最新評論