加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2020-12-14 22:18:11 所属栏目:资源 来源:网络整理
导读:方法一:去重复数据 复制代码 代码如下: 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; } v
方法一:去重复数据
复制代码 代码如下:

<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','a','e','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=[],'f','g'];
document.write('原始数组:'+x);
document.write("<br />");
document.write('去重复后:'+x.distinct());
</script>

您可能感兴趣的文章:

  • JS合并数组的几种方法及优劣比较
  • JavaScript中合并数组的N种方法
  • JS实现合并两个数组并去除重复项只留一个的方法
  • JavaScript合并两个数组并去除重复项的方法
  • 原生JavaScript实现合并多个数组示例
  • JavaScript数组合并的多种方法
  • js数组去重的三种常用方法总结
  • JS实现数组去重方法总结(六种方法)
  • js算法中的排序、数组去重详细概述
  • JavaScript数组去重的五种方法
  • javascript数组去重3种方法的性能测试与比较
  • JS实现的合并多个数组去重算法示例

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读