当前位置:网站首页>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.
边栏推荐
猜你喜欢
parser = argparse.ArgumentParser() parsing
云服务器安装部署Nacos2.0.4版本
利用 nucleo stm32 f767zi 进行USART+DMA+PWM输入模式 CUBE配置
querystring模块
String comparison size in MySQL (date string comparison problem)
C语言入门小游戏—三子棋
Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields
Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
磷脂-聚乙二醇-酰肼,DSPE-PEG-Hydrazide,DSPE-PEG-HZ,MW:5000
PCL—点云数据分割
随机推荐
Debian 10 NTP 服务配置
UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
通过PS 2021 将网页图标抠下来
ModuleNotFoundError No module named 'xxx' possible solutions
kettle 安装与配置
mysql中如何查看表是否被锁
@DateTimeFormat注解
DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
Scientific research reagent DMPE-PEG-Mal dimyristoylphosphatidylethanolamine-polyethylene glycol-maleimide
页面加载流程
SOCKS5
Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
猴子选大王(约瑟环问题)
Debian 10 NTP Service Configuration
磷脂-聚乙二醇-醛基 DSPE-PEG-Aldehyde DSPE-PEG-CHO MW:5000
C语言 结构体定义方法
A senior test engineer asked me these questions as soon as the interview came
C语言入门小游戏—三子棋
basic operator
Source Insight 使用教程(2)——常用功能