当前位置:网站首页>ES6-- 集合
ES6-- 集合
2022-06-25 21:50:00 【攀登程序猿】
Set
特点:
(1)去重,有添加add 删除delete 检测 has() 情况 clear 遍历 for (let item of set)
案例1:原始数据 去重、交集、并集、差集
// 去重
let arr = [1, 2, 3, 3, 3, 4, 4, 5, 6]
let unique = new Set(arr)
// 交集
let arr2 = [4, 5, 6, 5, 6]
let intersection = [...new Set(arr)].filter(item => {
if (new Set(arr2).has(item)) {
return true
} else {
return false
}
});
// 并集
let union = new Set([...arr, ...arr2])
// 差集
let diff = [...new Set(arr2)].filter(item => !new Set(arr).has(item))案例2:对象数据的去重
let arr = [
{id: 1, name: '周瑜'},
{id: 3, name: '王昭君'},
{id: 2, name: '亚瑟'},
{id: 1, name: '小乔'},
{id: 2, name: '大桥'},
{id: 3, name: '韩信'}
];
let obj = {}
arr = arr.reduce((preVal,curVal) => {
obj[curVal.id] ? "" : obj[curVal.id] = preVal.push(curVal)
return preVal
},[])
console.log(arr)Map
特点
(1)Map 对象存有键值对,其中的键可以是任何数据类型。
(2)Map 对象记得键的原始插入顺序。
(3)Map 对象具有表示映射大小的属性
边栏推荐
- Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
- Sqlmap learning (sqli labs as an example)
- Why absolute positioning overlaps
- Unity技术手册 - 粒子发射和生命周期内速度子模块
- Privatization lightweight continuous integration deployment scheme -- 03 deployment of Web services (Part 2)
- 2022-2028 global horizontal reciprocating compressor industry research and trend analysis report
- EVC, VVC, lcevc test: how about the performance of the latest MPEG codec?
- Practice of product library platform nexus of Devops
- 2022-2028 global TFT touch screen industry research and trend analysis report
- Leetcode topic [array] -18- sum of four numbers
猜你喜欢

记|一次exists关键字的学习记录
![[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF](/img/a1/09d2dc0ec47c54530da4d42d218d1c.jpg)
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF

Ribbon core ⼼ source code analysis

2022-2028 global carbon fiber unidirectional tape industry research and trend analysis report

Zero Trust: break the passive development mode of "attack and defense" and build a "moat" for enterprise safety

Three layer architecture + routing experiment

民航局:到 2025 年我国将初步建成安全、智慧、高效和绿色的航空物流体系

2022 love analysis · panoramic report of it operation and maintenance manufacturers

2022-2028 global RBI platform industry research and trend analysis report

2022-2028 global cloud based remote browser isolation industry research and trend analysis report
随机推荐
简单好用的缓存库 gcache
Analysis report on market demand situation and investment direction of China's optical transmission equipment industry from 2022 to 2028
用idea建立第一個網站
Factorymethod factory method
Thinking while walking
Dio encapsulated by the flutter network request (cookie management, adding interceptors, downloading files, exception handling, canceling requests, etc.)
Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
Pycharm 2022.1 EAP 2 release
Openwrt (VIII) application layer development
在线CRUDhasone关联查询报错Unabletouseinternalvariable:List
2022-2028 global DC linear variable differential transformer (LVDT) industry survey and trend analysis report
Youku IPv6 evolution and Practice Guide
Yyds dry goods inventory CEPH installation visual dashboard
Travel notes of 2022giao
TLog 助力盘古框架实现微服务链路日志追踪
Report on development status and prospects of global and Chinese coating industry strategic planning proposal 2022-2028
Raspberry PI (bullseye) replacement method of Alibaba cloud source
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF
Webrtc crash course
Yyds dry goods inventory JD 2, why is redis so fast?