国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JS數組去重與取重的示例代碼

2024-05-06 15:59:50
字體:
來源:轉載
供稿:網友
本篇文章主要是對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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 海南省| 北海市| 横山县| 师宗县| 古浪县| 天气| 洛浦县| 衡南县| 渭源县| 什邡市| 新宾| 本溪| 潜江市| 弥渡县| 佛坪县| 河津市| 天柱县| 米易县| 北票市| 雷波县| 云林县| 栖霞市| 皮山县| 黑龙江省| 竹北市| 永济市| 玛沁县| 武威市| 揭西县| 攀枝花市| 延津县| 宜都市| 梅州市| 钟山县| 耒阳市| 磴口县| 广德县| 全州县| 日喀则市| 林周县| 南丰县|