源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JS数组去重与取重的示例代码

  • 时间:2020-02-27 21:40 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JS数组去重与取重的示例代码
[b]方法一:去重复数据 [/b]
[u]复制代码[/u] 代码如下:
<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>
[b]方法二:取重复数据 [/b]
[u]复制代码[/u] 代码如下:
<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>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部