为Array对象添加一个去除重复项的方法
输入例子
[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN].uniq()
输出例子
[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a']
分析
Array.prototype.uniq = function () {
var arr = [];
var flag = true;
this.forEach(function(item) {
// 排除 NaN (重要!!!)
if (item != item) {
flag && arr.indexOf(item) === -1 ? arr.push(item) : '';
flag = false;
} else {
arr.indexOf(item) === -1 ? arr.push(item) : ''
}
});
return arr;
}
验证
我们只需要在数组上直接调用 uniq 方法就可以了,如:
[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN].uniq()
结果为:
[false, true, undefined, null, NaN, 0, 1, {}, {}, 'a']
进阶
ES6的实现
ES6新增了 Set 对象,也就是我们所说的“集合”,它类似于数组,但是成员 的值都是唯一的,没有重复的值。所以可以方便去重。
Set本身是一个构造函数,用来生成Set数据结构。(详看Set和Map数据结构)
如果用ES6为 Array 对象添加一个去除重复项的方法,则可以如下实现:
Array.prototype.uniq = function() {
return Array.from(new Set(this));
}
代码中用 Array.from 把 Set 结构转换成数组,当然,你也可以用其他方法, 这里不深究。这里去重关键代码只需要一行,是不是非常简单???
如果你要优雅一点,可以使用 ES6 的扩展运算符。如下:
Array.prototype.uniq = function() {
return [...new Set(this)];
}
原文链接:https://microzz.com/2017/04/01/array-uniq/
为Array对象添加一个去除重复项的方法的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- C#- 泛型去除重复项
今天被这个问题纠结了好一会.如何去除重复项,我遇到的问题是,在判断是否重复的条件是有两个,一个信息来源,一个是信息标题. 最后使用了哈希后很好的解决,感觉挺高效的.代码贴下,做一个备忘 //防止群发, ...
- java 去除重复项
import java.util.Arrays; import java.util.HashSet; import java.util.Set; class Demo20 { public stati ...
- Excel2003 去除重复项
利用 数据透视表 间接 获得 非重复项 1] 选中要去除重复项 的列 数据 2] 3]将选中列移动到 左侧 即可 4] 或者导入到Access中,用sql 语句中的 distinct SELECT D ...
- txt文本怎么去除重复项
txt文本怎么去除重复项?做网络推广的朋友经常会遇到这样的问题,txt文本文件里面有许多人名或者电话号码用来发送邮件或者短信,通常有许多是重复的,下面我来介绍两个方法来去除重复项,以人名为范本讲解. ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
随机推荐
- springboot拦截器的拦截配置和添加多个拦截器
在spring2.0之前的版本大部分都采用extends WebMvcConfigurerAdapter,把拦截器配置成一个bean,具体的方法,我不细说,网上一大堆.而在spring2.0之后,这个 ...
- Windows API 25篇 TerminateProcess
导语:结束一个进程的方法通常有:exit(), ExitProcess, TerminateProcess. 通常一个进程在正常情况下结束的话,系统会调用 ExitProcess函数结束进程,但有时候 ...
- virtualbox 启动虚拟机提示Cannot load R0 module
Cannot load R0 module C:\Program Files\Oracle\VirtualBox/VBoxDDR0.r0: SUPR3LoadModule: supLoadModule ...
- LUGOU P3374 【模板】树状数组 1(CDQ 分治)
传送门 拿个二维偏序练练cdq板子,其实就和归并排序差不多,复杂度不太会,似乎nlogn?. #include<iostream> #include<cstdio> #incl ...
- spring cloud深入学习(八)-----配置中心svn示例和refresh
svn版本 同样先示例server端的代码,基本步骤一样. 1.添加依赖 <dependencies> <dependency> <groupId>org.spri ...
- 《DSP using MATLAB》Problem 7.32
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- LA3695 Distant Galaxy
Distant Galaxy https://vjudge.net/problem/UVALive-3695 You are observing a distant galaxy using a te ...
- 跟我一起了解koa(三)
跟我一起了解koa中间件 一.是 什么是 Koa 的中间件 通俗的讲: :中间件就是匹配路由之前或者匹配路由完成做的一系列的操作,我们就可以 把它叫做中间件. 在 在express件 中间件( (Mi ...
- html 输入框显示“小叉叉”的清空方法
在IE10以下,我们的输入框input会出现小叉叉.怎么解决这个问题呢? 针对input框我们做一个处理 <style type="text/css"> input:: ...
- CentOS如何升级openssl到最新版本
本文不再更新,可能存在内容过时的情况,实时更新请移步原文地址:CentOS如何升级openssl到最新版本: 环境信息 CentOS Linux release 7.6.1810 (Core): Op ...