当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
--fs模块--
mysql中json类型字段用法
活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
C语言的变长数组
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
npm--package.json---require
微信小程序实现文本安全监测
Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
Chemical reagent Phospholipid-polyethylene glycol-hydroxyl, DSPE-PEG-OH, DSPE-PEG-Hydroxyl, MW: 5000
DOM操作---放大镜案例
解决MySQL创建子视图并查看的时候,字符集报错问题
debian 10 nat and routing forwarding
这些JS题面试时一定要答对!
mysql阶段总结
js 取字符串中某位置某特征的值,如华为(Huawei)=>华为
第一篇博客
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
磷脂-聚乙二醇-巯基,DSPE-PEG-Thiol,DSPE-PEG-SH,MW:5000
STM32 map文件解析
Detailed explanation of the usage of exists in mysql









