当前位置:网站首页>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.
边栏推荐
- oracle内连接和外连接
- 语义分割标签即像素值的巨坑,transforms.ToTensor()的错误使用
- 猴子选大王(约瑟环问题)
- AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
- 错误:with open(txt_path,‘r‘) as f: FileNotFoundError: [Errno 2] No such file or directory:
- querystring模块
- Usage of JOIN in MySQL
- @Autowired详解[email protected]在static属性上的使用
- STM32 map文件解析
- How to check whether a table is locked in mysql
猜你喜欢
随机推荐
querystring模块
@Autowired与@Resource区别
由中序遍历和前序遍历得到后序遍历(树的遍历)
---静态页面---
科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
PCL—点云数据分割
广州华为面试总结
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰活性基团
动态代理工具类
猴子选大王
利用 nucleo stm32 f767zi 进行USART+DMA+PWM输入模式 CUBE配置
这些JS题面试时一定要答对!
Dynamic proxy tool class
np.unique() function
PCL—point cloud data segmentation
Debian 10 NTP Service Configuration
@Autowired注解的使用
mysql中如何查看表是否被锁
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
【博学谷学习记录】超强总结,用心分享 | 软件测试 接口测试基础