当前位置:网站首页>Common methods of js array deduplication
Common methods of js array deduplication
2022-08-02 03:39:00 【suzhiwei_boke】
How to deduplicate an array?
This question has come up several times, and many interviewers are not satisfied that you can only give one or two ways.
ES6 provides a new data structure Set.It is similar to an array, but the values of the members are all unique and there are no duplicate values.
let unique= [...new Set(array)]; //The es6 Set data structure is similar to an array, the member values are unique, and duplicate values will be automatically deduplicated.//Set uses === internally to judge whether they are equal, similar to '1' and 1 will save both, NaN and NaN will only save one
2. Traverse, add the value to the new array, Use indexOf() to determine whether the value exists, and if it already exists, it will not be added to achieve the effect of deduplication.
let a = ['1','2','3',1,NaN,NaN,undefined,undefined,null,null, 'a','b','b'];let unique= arr =>{let newA=[];arr.forEach(key => {if( newA.indexOf(key)<0 ){ //Traverse whether there is a key in newA, if there is a key greater than 0, skip the push stepnewA.push(key);}});return newA;}console.log(unique(a)) ;//["1", "2", "3", 1, NaN, NaN, undefined, null, "a", "b"]//ps: This method cannot distinguish NaN, two NaN will appear.There is a problem, the following method is better.
3. Traverse, add the value of the array to the property name of an object, and assign a value to the property. The same property name cannot be added to the object. Based on this, the array can be deduplicated, and thenUse Object.keys(object) to return an array of enumerable properties of this object, which is the deduplicated array.
let a = ['1', '2', '3', 1,NaN,NaN,undefined,undefined,null,null, 'a', 'b', 'b'];const unique = arr => {var obj = {}arr.forEach(value => {obj[value] = 0;//In this step, a new attribute is added and assigned, if not assigned, the attribute will not be added})return Object.keys(obj);//`Object.keys(object)` returns an array of enumerable properties of this object, which is the deduplicated array}console.log(unique(a));//["1", "2", "3", "NaN", "undefined", "null", "a", "b"]This method will turn number, NaN, undefined, null, into string form, because the property name of the object is a string.Simple and most effective.
边栏推荐
- 磷脂-聚乙二醇-酰肼,DSPE-PEG-Hydrazide,DSPE-PEG-HZ,MW:5000
- 微信小程序实现文本安全监测
- 动态代理工具类
- 最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机
- np.unique() function
- 科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
- Chemical reagent Phospholipid-polyethylene glycol-hydroxyl, DSPE-PEG-OH, DSPE-PEG-Hydroxyl, MW: 5000
- DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group
- 解决glob()返回文件排序不一致问题&onnx本地按照安装方法
- __dirname
猜你喜欢

啃瓜记录又一天

@DateTimeFormat注解

MySql创建数据表
[email protected]在static属性上的使用"/>@Autowired详解[email protected]在static属性上的使用

canvas--饼状图

yolov5调用ip摄像头时出现的问题

Cut out web icons through PS 2021

UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu

Source Insight 使用教程(2)——常用功能

The @autowired distinguished from @ the Resource
随机推荐
Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案
DSPE-PEG-DBCO 磷脂-聚乙二醇-二苯并环辛炔 一种线性杂双官能聚乙二醇化试剂
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/yolov5-5.0/models/commo
@Autowired注解的使用
oracle内连接和外连接
解决glob()返回文件排序不一致问题&onnx本地按照安装方法
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
@Autowired与@Resource区别
环形链表---------约瑟夫问题
--fs模块--
sh: 1: curl: not found
npm--package.json---require
The usage of json type field in mysql
暴力方法求解(leetcode14)查找字符串数组中的最大公共前缀
由中序遍历和前序遍历得到后序遍历(树的遍历)
磷脂-聚乙二醇-叠氮,DSPE-PEG-Azide,DSPE-PEG-N3,MW:5000
【装机】老毛桃的安装及使用
C语言中关于2的n次方求值问题(移位运算)
微信小程序实现文本安全监测
骨架效果 之高级渐变,适用图片等待时