常用数组去重(九种)

简介: 数组

常用数组去重(九种)

项目中数组去重应该是随处可见,今天稍作整理了下(vue)

data() {
   return {
      oldArray: ["我们的","我们的",'1','abc','abc', 1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, {},{}]
   };
}
方法一:两个for循环,然后splice去重(ES5中最常用)//NaN和{}没有去重
for() {
    for(var i=0; i<this.oldArray.length; i++){
        for(var j=i+1; j<this.oldArray.length; j++){
            //第一个等同于第二个,splice方法删除第二个
                if(this.oldArray[i] === this.oldArray[j]){
                    this.oldArray.splice(j,1);
                    j--;
                }
            }
        }
        console.log(this.oldArray); // {0: '我们的', 1: '1', 2: 'abc', 3: 1, 4: 'true', 5: true, 6: 15, 7: false, 8: undefined, 9: null, 10: NaN, 11: NaN, 12: 'NaN', 13: 0, 14: {…}, 15: {…}}
}
方法二:indexof NaN和{}没有去重
indexOf() {
      const newArray = [];
      this.oldArray.forEach((item) => {
        return newArray.indexOf(item) === -1 ? newArray.push(item) : ''
      })
      console.log(newArray); // ['我们的', '1', 'abc', 1, 'true', true, 15, false, undefined, null, NaN, NaN, 'NaN', 0, Proxy, Proxy]
},
方法三: Set去重:不考虑兼容性,这种去重的方法代码最少。(ES6写法,现在利用babel,基本都可以兼容了) 无法去掉“{}”空对象
set() {
      const newArray = Array.from(new Set(this.oldArray)) || [...new Set(this.oldArray)];
      console.log(newArray); // ['我们的', '1', 'abc', 1, 'true', true, 15, false, undefined, null, NaN, 'NaN', 0, Proxy, Proxy]
},
方法三:indexof 无法去掉“{}和NaN”空对象
indexOf() {
      const newArray = [];
      this.oldArray.forEach((item) => {
        return newArray.indexOf(item) === -1 ? newArray.push(item) : ''
      })
      console.log(newArray); // ['我们的', '1', 'abc', 1, 'true', true, 15, false, undefined, null, NaN, NaN, 'NaN', 0, Proxy, Proxy]
},
方法四:利用 includes {}没有去重
includes() {
      const newArray = [];
      this.oldArray.forEach((item) => {
        return newArray.includes( item) ? '' :newArray.push(item);
      })
      console.log(newArray); // ['我们的', '1', 'abc', 1, 'true', true, 15, false, undefined, null, NaN, 'NaN', 0, Proxy, Proxy]
},
方法五:利用 filter {}没有去除 ,NaN合并成字符串
filter() {
      const newArray = this.oldArray.filter((item, index, arr) => {  //当前元素,在原始数组中的第一个索引==当前索引值,否则返回当前元素
        return arr.indexOf(item, 0) === index;
      })
      console.log(newArray); // ['我们的', '1', 'abc', 1, 'true', true, 15, false, undefined, null, 'NaN', 0, Proxy, Proxy]
},
方法六:利用 hasOwnProperty 注意:如果两个对象不一样,不能比较对象里的内容
hasOwnProperty() {
      const obj = {};
      const newArray = this.oldArray.filter((item) => {
        return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)
      })
      console.log(newArray); // ['我们的', '1', 'abc', 1, 'true', true, 15, false, undefined, null, NaN, 'NaN', 0, Proxy]
},
方法七:利用reduce+includes {}没有去除
reduce() {
      const newArray = this.oldArray.reduce((prev,cur) => prev.includes(cur) ? prev : [...prev,cur],[]);
      console.log(newArray); // ['我们的', '1', 'abc', 1, 'true', true, 15, false, undefined, null, NaN, 'NaN', 0, Proxy, Proxy]
},
方法八:利用Map数据结构去重,利用Map的两个方法has和set来判定数组中的每个元素(循环判定是否有该元素,没有的时候,set该元素为true。新数组再添加)在输出一下构造函数Map。 {}没有去除
map() {
      let map = new Map();
      const newArray = [];
      for (let i = 0; i < this.oldArray.length; i++) {
        if (!map.has(this.oldArray[i])) {
          map.set(this.oldArray[i], true);
          newArray.push(this.oldArray[i]);
        }
      }
      console.log(newArray); // ['我们的', '1', 'abc', 1, 'true', true, 15, false, undefined, null, NaN, 'NaN', 0, Proxy, Proxy]
},
方法九:利用sort方法 {}和NaN没有去除
sort() {
        //存入排序过后的数组
        let formArr = this.oldArray.sort();
        //定义一个新数组,并使新数组中的第一个元素等于原数组中的第一个元素
        let newArray = [formArr[0]];
        for (let i = 1; i < formArr.length; i++) {
            if (formArr[i] !== formArr[i - 1]) {
                newArray.push(formArr[i]);
            }
        }
        console.log(newArray); // [0, '1', 1, 15, NaN, NaN, 'NaN', Proxy, Proxy, 'abc', false, null, 'true', true, '我们的', undefined]
},

如果有更好,或者不一样的方法,欢迎交流!

目录
相关文章
|
Java Linux iOS开发
又一款 IDEA 全家桶 神器 ja-netfilter-all 插件
又一款 IDEA 全家桶 神器 ja-netfilter-all 插件
10336 121
又一款 IDEA 全家桶 神器 ja-netfilter-all 插件
|
API
swagger文档使用常用注解
swagger文档使用常用注解
824 0
|
前端开发 Java 测试技术
【IDEA+通义灵码插件】实现属于你的大模型编程助手
【IDEA+通义灵码插件】实现属于你的大模型编程助手
4156 0
|
Cloud Native Java Go
解决 Spring Boot 与 springfox 的 NullPointerException 问题
解决 Spring Boot 与 springfox 的 NullPointerException 问题
811 0
|
消息中间件 缓存 算法
Java8 快速实现List转map 、分组、过滤等操作
利用java8新特性,可以用简洁高效的代码来实现一些数据处理。
|
Linux
你知道如何在Linux操作系统上添加虚拟IP吗?这篇文章帮你搞定
在高并发和高可用的场景中,往往会搭建服务器集群,那么如何将多台服务器的IP映射成一个IP地址呢?本文就帮你搞定这个问题。
641 0
你知道如何在Linux操作系统上添加虚拟IP吗?这篇文章帮你搞定
|
7天前
|
存储 弹性计算 缓存
阿里云服务器租赁费用:新版租赁收费标准及活动报价参考
本文更新了2026年阿里云全系列云服务器租赁活动报价,所有特惠资源均可前往阿里云活动中心选购,整体覆盖从个人入门到企业级高性能场景的全梯度需求。其中轻量应用服务器主打极致性价比,2核2G峰值200M带宽配置每日10点、15点限时抢购价仅38元/年,2核4G配置379元/年起;高性价比的经济型e实例、通用算力型u2i实例覆盖2核4G至4核32G全档位,适配开发测试与中小型企业业务;搭载英特尔至强6处理器的第九代c9i企业级实例算力较上代提升20%,支撑高并发生产环境,不同实例规格价差清晰,用户可根据自身业务负载与预算灵活选型。
1728 116
|
8天前
|
人工智能 程序员 API
Codex 接入 DeepSeek-V4-Flash:还能补上识图,提供两套方案
Codex 接入 DeepSeek-V4-Flash 怎么配?本文覆盖 CLI 与桌面端,再用 qwen3-vl-flash 补识图,两套方案可直接照做
1210 8
|
14天前
|
云安全 人工智能 运维
阿里云联动百位企业安全专家,共识Agent防御最佳实践
当Agent成为新员工,你的安全边界在哪里?
1956 9
阿里云联动百位企业安全专家,共识Agent防御最佳实践