当前位置:网站首页>ES6 from entry to master # 10: Set the Set data type
ES6 from entry to master # 10: Set the Set data type
2022-07-29 15:33:00 【Kay Xiaomo】
说明
ES6 从入门到精通系列(全23讲)学习笔记.
Set 集合
集合:表示无重复值的有序列表
let kaimo = new Set();
console.log(kaimo)

添加元素
let kaimo = new Set();
kaimo.add(3);
kaimo.add("1");
kaimo.add(3);
kaimo.add([3, 1, 3])
console.log(kaimo)

删除元素
kaimo.delete([3, 1, 3]);
console.log(kaimo)
kaimo.delete(3);
console.log(kaimo)

Check if a value is in set 中
kaimo.has('1')

访问集合的长度
kaimo.size

注意:forEach 的用法,这里的 val 是相等于 key 的
kaimo.forEach((val, key) => {
console.log("val--->", val)
console.log("key--->", key)
})

Convert a collection to an array using the spread operator
let kaimo = new Set([3, 1, 3]);
console.log(kaimo )
let arr = [...kaimo];
console.log(arr)

拓展
set 中对象的引用无法被释放
let kaimo = new Set(), obj = {
name: "kaimo313"};
kaimo.add(obj)
// 释放当前的资源
obj = null
console.log(kaimo);

解决方法:使用 WeakSet
let kaimo1 = new Set(), obj1 = {
name1: "kaimo313"};
kaimo1.add(obj1)
// 释放当前的资源
obj1 = null
let kaimo2 = new WeakSet(), obj2 = {
name2: "kaimo313"};
kaimo2.add(obj2)
// 释放当前的资源
obj2 = null
kaimo1
kaimo2

WeakSet 的特点
- Arguments of non-object types cannot be passed

- 不可迭代

- 没有 forEach()
- 没有 size 属性
边栏推荐
- 极市直播丨严彬-Unicorn:走向目标跟踪的大一统(ECCV2022 Oral)
- WOLFLAB一方老师为什么要写网络虚拟化《VMware NSX-T卷2》路由架构-2
- c语言字符和字符串总结
- 详解微处理器CPU的系统结构
- 论人生自动化
- arcgis中编码方式改变引起的shp文件乱码、字符截断问题处理
- Guangzhou Emergency Management Bureau released the top ten safety risks of hazardous chemicals in summer
- Immediate experience with CTS - | D further promotion application equipment compatibility
- The raised platform system based on JSP&Servlet implementation
- 数据中台建设(四):企业构建数据中台评估
猜你喜欢
随机推荐
文件管理:文件的物理结构
【LeetCode】217. 存在重复元素
KDD'22博士论文奖:清华裘捷中成亚洲高校首位获得者,斯坦福Rex Ying获WINNER奖
BGP federation experiment
数据分析(二)
RestTemplate下载文件的另一种方式
文件管理:文件的逻辑结构
【ArcGIS微课1000例】0030:ArcGIS利用MXD doctor工具分析并修复mxd地图文档
瑞萨RZ/G2L处理器详细测评
Why does APP use the JSON protocol to interact with the server: serialization related knowledge
使用Xshell和Xftp7跑学校服务器记录
UFLO:5、启动任务并自动完成第一个人工任务
Couldn‘t create temporary file /tmp/apt.conf.uko4Kd for passing config to apt-key
gateway基本使用
Based on domestic, link global | schneider electric "industrial SI alliance partners hand in hand" to the industry in the future
数据中台建设(四):企业构建数据中台评估
又一位AI大佬辞职进体制内!AI的尽头是编制?
【 LeetCode 】 217. Duplicate elements
VMware 16.1软件安装包下载及安装教程
基于C语言仿真实现的粒子火焰系统









