Array 数组去重 总结10方法(7)
1,常规双循环去重(缺点:循环次数较多)
Array.prototype.unique1 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0;let res = [that[0]];for(let i = 1; i < len; i++){let falg = false;for(let j = 0; j < res.length; j++){if(that[i] === res[j]){falg = true;break;}}if(!falg){res.push(this[i]);}}return res;
}
注意:
(1,必须在第二个循环外push到新的数组
(2,减少循环次数,在第二个循环中找到相等值,马上退出该循环
(3,每次循环对falg检验
(4,由于第一值直接赋值,所以不用检测第一个值
2,数组的sort先排序再去重(缺点:返回数组为排序后的顺序)
Array.prototype.unique2 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this).sort(),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(that[i] !== res[res.length - 1]){res.push(that[i]);}}return res;
}
3,对象键值不重复(缺点:占内存)
Array.prototype.unique3 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,obj = {},res = [];for(let i = 0; i < len; i++){let type = typeof that[i];if(!obj[that[i]]){res.push(that[i]);obj[that[i]] = [type];}else if(obj[that[i]].indexOf(type) === -1){res.push(that[i]);obj[that[i]].push(type);}}return res;
}
4,indexOf检测新数组(优点:简单)
Array.prototype.unique4 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(res.indexOf(that[i]) === -1){res.push(that[i]);}}return res;
}
5,indexOf检测原数组
Array.prototype.unique5 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(that.indexOf(that[i]) === i){res.push(that[i]);}}return res;
}
6,数组的every方法
Array.prototype.unique6 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(res.every(function(val){return val !== that[i]})){res.push(that[i]);}}return res;
}
注意:如果发现了一个这样的元素,every 方法将会立即返回 false。否则,callback 为每一个元素返回 true,every 就会返回 true。
7,数组的some方法
Array.prototype.unique10 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(!res.some(function(val){return val === that[i]})){res.push(that[i]);}}return res;
}
注意:如果找到了这样一个值,some 将会立即返回 true。否则,some 返回 false。
8,数组的includes方法
Array.prototype.unique7 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(!res.includes(that[i])){res.push(that[i]);}}return res;
}
9,数组的filter方法
Array.prototype.unique8 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(res.filter(function(val){return val === that[i]}).length === 0){res.push(that[i]);}}return res;
}
10,数组的find方法(缺点:没有做0或者false这个一类判断)
Array.prototype.unique9 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(!res.find(function(val){return val === that[i]})){res.push(that[i]);}}return res;
}
还有lastIndexOf,findIndex等方法也能做去重,就不一一列举,有兴趣的可以自己做一下。
其他
[我的博客,欢迎交流!](http://rattenking.gitee.io/stone/index.html)
[我的CSDN博客,欢迎交流!](https://blog.csdn.net/m0_38082783)
[微信小程序专栏](https://blog.csdn.net/column/details/18335.html)
[前端笔记专栏](https://blog.csdn.net/column/details/18321.html)
[微信小程序实现部分高德地图功能的DEMO下载](http://download.csdn.net/download/m0_38082783/10244082)
[微信小程序实现MUI的部分效果的DEMO下载](http://download.csdn.net/download/m0_38082783/10196944)
[微信小程序实现MUI的GIT项目地址](https://github.com/Rattenking/WXTUI-DEMO)
[微信小程序实例列表](http://blog.csdn.net/m0_38082783/article/details/78853722)
[前端笔记列表](http://blog.csdn.net/m0_38082783/article/details/79208205)
[游戏列表](http://blog.csdn.net/m0_38082783/article/details/79035621)
转载于:https://www.cnblogs.com/linewman/p/9918546.html
Array 数组去重 总结10方法(7)相关推荐
- Javascript中数组去重的六种方法
数组去重 第一种方法: 先对数组进行排序sort(),排好序,然后把数组的当前项和后一项进行比较,相同则使用数组的splice(相同的位置,1),但是为了防止数组塌陷,每次删除数组元素的时候要把i的值 ...
- javascript数组去重的10种方法
亲爱的小伙伴,对于数组javascript中的数组去重方法你知道多少种呢?学会如何对数组进行去重对于javascript的学习来说也是十分重要的,下边就让我来分享一下我所知道的集中数组去重的方法吧! ...
- 史上最全JavaScript数组去重的十种方法(推荐)
一.前言: 我们在实际工作中,或者在面试找工作时,都会用到或者被问到一个问题,那就是"数组如何去重".是的,这个问题有很多种解决方案,看看下面的十种方式吧! 二.数组去重方式大汇总 ...
- php两个数组之间去重,php数组去重、魔术方法、redis常用数据结构及应用场景
一.用函数对数组进行去重的方法 1. arrau_unique函数的作用 移除数组中重复的值. 将值作为字符串进行排序,然后保留 每个值第一次 出现的健名,健名保留不变. 第二个参数可以选择排序方式: ...
- JavaScript数组去重6种方法
数组去重涉及基础知识较多,总结了以下6个方法: 双重for循环,push新数组: 双重for循环,splice原数组: 单个for循环,遍历对象属性: 单个for循环,sort排序后遍历: ES5,i ...
- 超全的数组去重12种方法
前言 数组去重,可以说是一个比较常见的面试题,今天来盘点一下都有哪些方法可以实现数组去重. 方法1.双重for循环 这是一个最笨的方法,双重循环. var arr = [1, 2, 3,4 ,5,6, ...
- js数组去重(多种方法)
1 // js数组去重 2 Array.prototype.fun1 = function(){ 3 var arr = this, 4 result = [], 5 i, 6 len = arr.l ...
- (PASS)JAVA数组去重 三种方法 (不用集合)
第一种方法(只学到数组的看): 定义一个新的数组长度和旧数组的长度一样,存储除去重复数据的旧数组的数据和0, package demo01;import java.sql.Array; import ...
- 数组去重的各种方法速度对比
首先需要一个自动生成数组的函数 // 自动生成数组的函数function randomArr (n) {let arr = [];for (let i = 1; i <= n; i++) {ar ...
最新文章
- 看完这些能控制大脑的寄生虫,你会怀疑人类!
- 「译」在JavaScript中将值转换为字符串的5种方法
- 二、传统数据库遇到的挑战
- css3鼠标果果变手型代码_css3 鼠标悬浮动画效果
- Linux安装Elasticsearch-head插件
- MFC源码不能设置断点调试
- 《CLIP2Video》-腾讯PCG提出CLIP2Video,基于CLIP解决视频文本检索问题,性能SOTA!代码已开源!...
- SPOJ QTREE
- 【课程笔记】南大软件分析课程—16课时完整版
- javascript指定日期增加自然月份(有闰年2月判断)
- 颜文字风波+选颜文字游戏
- 【PCL】NDT点云配准(Registration)
- 2018第四届美亚杯全国电子数据取证大赛团队赛wp
- matlab电学成像,利用MATLAB进行电磁学计算及可视化教学.PDF
- 最大子序列和问题c语言力扣,力扣
- 计算机网络四交换技术,计算机网络数据交换技术发展
- 截止2021年企业公众号开通数据(60万+记录)
- 网上邻居不能访问问题集锦
- 让Android程序教你画画
- Linux 探索之旅 | 第五部分第八课:用 Shell 做统计练习
热门文章
- 设置普通用户执行docker命令,执行docker命令无需输入密码或者切换root用户
- Docker compose 容器编排
- Python+OpenCV 图像处理系列(3)—— 画线、矩形、画圆、画椭圆、画多边形
- Warshall算法多源点之间最短路径的算法最短距离
- 在Yolov5 Yolov4 Yolov3 TensorRT 实现Implementation
- 嵌入式Linux设备驱动程序:编写内核设备驱动程序
- H.265 HD 和H.265 4K Video Encoder IP Core
- 2021年大数据Flink(十三):流批一体API Sink
- [C] Bellman-Ford边松弛:解决负权边
- logcat --pid xx 查看某个进程的信息