当前位置:网站首页>(JS) array de duplication
(JS) array de duplication
2022-06-29 11:14:00 【Yu Cainiao, who asked not to be named】
- Use new Set()
var arr = [1,2,3,4,5,5,4,3,2,11,3,22,11,11,22];
let c = new Set(arr);
new Set() Method can add all non repeating values to the new Set in , Then return
- Use fliter() Method
var arr = [1,2,3,4,5,5,4,3,2,11,3,22,11,11,22];
let x = arr.filter(function(val,index),array) {
return array.indexOf(val)=== index;
}
filter() Method can return the value that meets the condition . Use indexOf lookup array Whether the index of the current value in is equal to index, If equal to, it means that the array does not exist val, Return once , If there is , Then don't go back .
- Use circular judgment
var arr = [1,2,3,4,5,5,4,3,2,11,3,22,11,11,22];
let newArr = [];
for(let i = 0; i<arr.length; i++) {
if(newArr.indexOf(arr[i]) === -1) {
newArr.push(arr[i]);
}
}
Create a new empty array , And then use indexOf Judge , If it is equal to -1, Indicates that there is no current item in the empty array , be push Enter new array . This method can only judge simple type values , If judgment is needed undefined and NaN,Null You need to add additional judgment conditions .
- Use reduce() Method to remove duplication
var arr = [1, 2, 3, 4, 5, 5, 4, 3, 2, 11, 3, 22, 11, 11, 22];
let x = arr.reduce(function(accumulation, current) {
if (!accumulation.includes(current)) {
accumulation.push(current);
}
return accumulation;
}, []);
reduce() The method is ES6 New array method added in , It can be used for cumulative calculation , It can also be used to judge some items
边栏推荐
- Limit introduction summary
- Online text filter less than specified length tool
- 在编写shell脚本时如何正确姿势地管理临时文件
- [digital signal modulation] realize signal modulation and demodulation based on am+fm+dsb+ssb, including Matlab source code
- what? It's amazing that you can read the whole comic book for free. You can't learn to be a money saver together
- How to obtain method parameter values through WinDbg
- Google Earth engine (GEE) - Gedi L2a vector canopy top height (version 2) global ecosystem data set
- 【数字信号调制】基于 AM+FM+DSB+SSB实现信号调制解调含Matlab源码
- Week 12 experiment -- implementation of VGA protocol based on FPGA
- 那些大佬经常在bash 命令行终端不经意间666飞起的必备操作
猜你喜欢

《Datawhale推荐系统教程》来了!

西门子S7-200SMART控制步进电机的具体方法及示例程序

TTL串口学习型红外遥控模块可扩展成网络控制

云原生开发必备:首个通用无代码开发平台 iVX 编辑器

Modbustcp protocol network learning single channel infrared module (double-layer board)

Using EasyX configuration in clion

what? It's amazing that you can read the whole comic book for free. You can't learn to be a money saver together

ModbusTCP协议WIFI无线学习型单路红外模块(小壳版)

Reids设计与实现

Modbus RTU protocol 485 learning 2-way infrared module
随机推荐
Thoroughly understand JVM Series 7: what is the flow process of objects in the generational model?
(JS)捕获错误(异常)
TTL串口学习型红外遥控模块可扩展成网络控制
在编写shell脚本时如何正确姿势地管理临时文件
(JS)isNaN()方法判断undefined为true的原因
Uboot for embedded driver development -- common command parameters in uboot
Modbus RTU protocol 485 learning 2-way infrared module
X-Frame-Options(点击劫持) 网页劫持漏洞
[various * * question series] what are OLTP and OLAP?
math_ Mathematical expression & deformation of equation equation & accumulation of combined operation skills / means
任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
(JS) array methods: slice and slice
BS-GX-018 基于SSM实现在校学生考试系统
由ASP.NET Core根据路径下载文件异常引发的探究
Google Earth Engine(GEE)——GEDI L2A Vector Canopy Top Height (Version 2) 全球生态系统数据集
How to obtain method parameter values through WinDbg
BS-GX-017基于SSM实现的在线考试管理系统
Online text filter less than specified length tool
[daily 3 questions (1)] judge the color of a grid on the chess board
嵌入式驱动开发之uboot---uboot 中的常见命令参数参数