js 数组去重方法汇总
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>js 数组去重</title>
</head>
<body> <script src="https://cdn.bootcss.com/lodash.js/4.17.10/lodash.min.js"></script>
<script type="text/javascript">
//方法一
function arrayUnique1(arr) {
const ret = []
for(let i = 0; i < arr.length; i++) {
const item = arr[i],
idx = ret.indexOf(item) if(idx < 0) {
ret.push(item)
}
}
return ret
}
//方法二
function arrayUnique2(arr) {
const map = {},
ret = []
for(let i = 0; i < arr.length; i++) {
const item = arr[i]
if(map[item] == null) {
map[item] = i
ret.push(item)
}
}
return ret
}
//方法三:
function arrayUnique3(arr) {
return arr.filter((item, i) => arr.indexOf(item) === i)
}
//方法四
function arrayUnique4(arr) {
return [...new Set(arr)]
}
const arr = [1, 3, 1, 2, 2, 4, 3, 4, 1]
console.log(arrayUnique4(arr))
</script>
</body> </html>
js 数组去重方法汇总的更多相关文章
- JavaScript 数组去重 方法汇总
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- 原声js数组去重方法
数组去重方法 方法一 ---- 利用数组filter + indexOf方法去重 方法二 ---- 利用数组forEach + indexOf方法去重 方法三 ---- 利用数组from方法 + Se ...
- js数组去重方法分析与总结
数组去重经常被人拿来说事,虽然在工作中不常用,但他能够很好的考察js基础知识掌握的深度和广度,下面从js的不同阶段总结一下去重的方法. ES3阶段 该阶段主要通过循环遍历数组从而达到去重的目的 多次循 ...
- JavaScript数组去重方法汇总
1.运用数组的特性 1.遍历数组,也遍历辅助数组,找出两个数组中是否有相同的项,若有则break,没有的话就push进去. //第一版本数组去重 function unique(arr){ var r ...
- js 数组清空 方法 汇总
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Js数组去重方法总结
//方法一 var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5]; function removeDuplicatedItem(arr) { for(var i = 0; ...
- 三种常用的js数组去重方法
第一种是比较常规的方法 思路: 1.构建一个新的数组存放结果 2.for循环中每次从原数组中取出一个元素,用这个元素循环与结果数组对比 3.若结果数组中没有该元素,则存到结果数组中 Array.pro ...
- js数组去重方法包括Es6(方法有很多,但是需要考虑兼容性和数据类型场景)
1.Es6提供的方法 <script type="text/javascript"> //ES6里新添加了两个方法,set(set是一种新的数据结构,它可以接收一个数组 ...
- js 数组去重方法总结
var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, ...
随机推荐
- 木材加工(LintCode)
木材加工 有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k.当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度. 样例 有3根木头[232 ...
- 倍增LCA学习笔记
前言 "倍增",作为一种二进制拆分思想,广泛用于各中算法,如\(ST\)表,求解\(LCA\)等等...今天,我们仅讨论用该思想来求解树上两个节点的\(LCA\)(最近公共祖先 ...
- SublimeCodeIntel代码自动补全配置
主要使用python3,所有配置以python3为例.其他语言同理.利用sublimeCodeIntel插件可以实现自动提示python3代码.跳转追踪自定义函数.查看系统函数等.功能还是相当强大的. ...
- 大素数判断(miller-Rabin测试)
题目:PolandBall and Hypothesis A. PolandBall and Hypothesis time limit per test 2 seconds memory limit ...
- 打开eclipse 时出现Failed to load the JNIshared libraryd的解决方式
由于电脑重装系统,所以电脑的jdk,与eclipse需要重新配置,今天配置完成jdk之后,打开eclipse出现了Failed to load the JNIshared libraryd的错误,如下 ...
- CocurrentHashMap和HashTable区别分析
集合是编程中最常用的数据结构.而谈到并发,几乎总是离不开集合这类高级数据结构的支持.比如两个线程需要同时访问一个中间临界区(Queue),比如常会用缓存作为外部文件的副本(HashMap).这篇文章主 ...
- 【构造】AtCoder Regular Contest 079 D - Decrease (Contestant ver.)
从n个t变化到n个t-1,恰好要n步,并且其中每一步的max值都>=t,所以把50个49当成最终局面,从这里开始,根据输入的K计算初始局面即可. #include<cstdio> # ...
- 【贪心】Codeforces Round #407 (Div. 2) A. Anastasia and pebbles
贪心地一个一个尽可能往口袋里放,容易发现和顺序无关. #include<cstdio> #include<iostream> using namespace std; type ...
- 【动态规划+二分查找】POJ2533&POJ1631最长上升子序列(LIS)
POJ2533裸的LIS,时间复杂度为O(n^2) #include<iostream> #include<cstdio> using namespace std; +; in ...
- android 关于Make sure the plugin is properly configured问题的解决办法
这个问题引发的原因最初的报错是: [2013-10-14 10:01:58 - XXX] The connection to adb is down, and a severe error has o ...