当前位置:网站首页>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 对象具有表示映射大小的属性
边栏推荐
- 2022-2028 global SiC igniter industry research and trend analysis report
- Ribbon core ⼼ source code analysis
- Use apiccloud AVM multi terminal component to quickly realize the search function in the app
- How to guarantee idempotency of message queue
- Initialization process of gstlibav
- Unity技术手册 - 生命周期旋转RotationOverLifetime-速度旋转RotationBySpeed-及外力
- 2022爱分析· IT运维厂商全景报告
- 图解栈帧运行过程
- 目前期货怎么开户安全些?哪些期货公司靠谱些?
- Dialog+: Audio dialogue enhancement technology based on deep learning
猜你喜欢
Intimacy - [comfortable exit] - final communication to reduce injury
2022-2028 global variable frequency compressor technology industry research and trend analysis report
圖解棧幀運行過程
Basic concepts of processor scheduling
In depth analysis of Flink fine-grained resource management
2022-2028 global industrial TFT LCD industry survey and trend analysis report
Youku IPv6 evolution and Practice Guide
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF
Three layer architecture + routing experiment
Diagram of stack frame running process
随机推荐
Analysis of gpl3.0 license software copyright dispute cases
Simple and easy-to-use cache library gcache
[dynamic programming] longest palindrome substring thinking about dynamic transfer cycle sequence
Privatization lightweight continuous integration deployment scheme -- 03 deployment of Web services (Part 2)
Analysis report on scale investigation and investment development suggestions of China's special equipment inspection and testing industry 2022-2028
你好,请问老师,在支付宝基金开户真的安全吗?
How to guarantee idempotency of message queue
[动态规划]最长回文子串-对于动态转移循环顺序的思考
Research and Analysis on the current situation of China's magnetic detector Market and forecast report on its development prospect (2022)
Some reflections on preparing for the Blue Bridge Cup
Basic concepts of processor scheduling
2022-2028 global horizontal reciprocating compressor industry research and trend analysis report
Yyds dry goods inventory JD 2, why is redis so fast?
Client cannot authenticate via:[TOKEN, KERBEROS]
Wpewebkit debugging MSE playback
华为云短信测了很多手机都提示发送频繁
GStreamer initialization and plugin registry procedures
Research Report on China's new energy technology and equipment market competition analysis and marketing strategy suggestions 2022-2028
Youku IPv6 evolution and Practice Guide
China coated abrasive tools industry market depth analysis and development strategy consulting report 2022-2028