当前位置:网站首页>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']
边栏推荐
- Nature: correlation between oscillatory signals and gene expression supporting human episodic memory coding
- Heavyweight review: strategies for reliable fMRI measurements
- 第一批00后下场求职:不要误读他们的“不一样”
- Application of map function in JS
- PHP composer yii2 installation
- Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
- Preliminary sequencing problem
- What are the advantages of the completely free and open source flutter? How to learn about flutter?
- [burning] Tencent cloud high tech computing platform HTPC cloud elastic cluster release!
- Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]
猜你喜欢

1058 multiple choice questions (20 points)

mysql常用指令

Fetch request details

R tree of search tree

【二分】leetcode1011. Capacity To Ship Packages Within D Days

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

线上MySQL的自增id用尽怎么办?

Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]

【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM
![Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]](/img/de/d73805aaf4345ca3d2a7baf85aab8d.jpg)
Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]
随机推荐
Flink practice tutorial: advanced 7- basic operation and maintenance
Goframe framework: log configuration management
Installing serverstatus probe using pagoda
2022-01-22: Li Kou 411, the abbreviation of the shortest exclusive word. Give a string number
Why not use math random() ?!
CentOS install redis
Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]
Ultra detailed Apache and PHP installation tutorial windows (2022.1)
Initialize MySQL Gorm through yaml file
Bi skills - authority control
[advanced Android] entrusted by kotlin
If there is a smart bus visualization platform, can "beginning" restart indefinitely?
centos7 安装 MySQL 及配置 innodb_ruby
Email authentication bypass
How to print array contents
JS to realize character case switching
Application of map function in JS
[advanced Android] kotlin delegate attribute
Weekly Postgres world news 2022w03
Parsing the implementation of easygbs compatible token as parameter passing