当前位置:网站首页>JS array de duplication, removing the same value
JS array de duplication, removing the same value
2022-06-23 03:35:00 【It workers】
ES5 Realization
JavaScript 1.6 / ECMAScript 5 You can use native methods filter To achieve array de duplication .
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter( onlyUnique ); // returns ['a', 1, 2, '1']Native filter Method will loop through the array , And pass the callback parameter to onlyUnique function .
onlyUnique It will check whether the callback value appears for the first time , If not , Will not be generated into the array .
This method does not require any additional libraries , for example jQuery or prototype.js.
This method is also applicable to mixed type arrays .
For not supporting filter or indexOf Old browser for method , We can consider give up .
Make fun of , You can refer to it MDN file , Find out about filter and indexOf Compatible solutions .
ES6
ES6 have access to Set To achieve array de duplication , Compared with ES5 The code will be simpler .
var myArray = ['a', 1, 'a', 2, '1']; let unique = [...new Set(myArray)]; // unique is ['a', 1, 2, '1']
边栏推荐
- LRU cache
- Application of map function in JS
- 页面导出excel的三种方式
- To implement a task scheduling system, it is enough to read this article
- Evolution of cloud firewall products
- [JS reverse hundreds of cases] the login of a HN service network is reverse, and the verification code is null and void
- Why don't I suggest you use "! = null" to judge empty space?
- Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]
- JS remove first character of string
- TRTC setaudioroute invalid problem
猜你喜欢

第一批00后下场求职:不要误读他们的“不一样”

Banknext microservice: a case study

Gakataka student end to bundle Version (made by likewendy)

【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM

【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现

One of the touchdesigner uses - Download and install

mysql常用指令

Detailed discussion on modular architecture design of MCU firmware

Static code block, code block, constructor execution order

innodb_ruby 视角下 MySQL 记录增删改
随机推荐
innodb_ruby 视角下 MySQL 记录增删改
Generate PDF417 code in batch through TXT file
MCU model selection for charging point system design
[metauniverse 7ai rope skipping] how is this app light application realized? What are the application scenarios?
Evolution of cloud firewall products
数据交易怎样实现
This point (II)
What is the difference between comparator and comparable?
What is the difference between ArrayList and LinkedList?
Why don't I suggest you use "! = null" to judge empty space?
Record an edusrc vulnerability mining
[advanced Android] kotlin delegate attribute
If there is a smart bus visualization platform, can "beginning" restart indefinitely?
[Alibaba middleware technology series] "Nacos technology" service registration and discovery related principle analysis
Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo
On the way home from the Spring Festival transportation, traffic visualization will escort you
JS counts the number of times a string appears in another string
Frequent actions to expand the scale of new energy industry Guangzhou plans to invest 1.4 billion in photovoltaic power generation projects
Is LinkedList a one-way linked list or a two-way linked list?
Learning summary of spanquery source code