返回

es6的Set太好用了

发布时间:2022-09-18 23:48:19 271
# php
let mySet = new Set();
//添加 add
mySet.add(1);
mySet.add(5);
mySet.add(5);
mySet.add('some text');
let o = { a: 1, b: 2 };
mySet.add(o);
mySet.add({ a: 1, b: 2 });
//集合中是否存在 has
const has = mySet.has(o);
//删除 delete
mySet.delete(5);
//集合的遍历迭代 for...of
for(let item of mySet) console.log(item);
//集合转数组 [...mySet] Array.from()
const myArr = [...mySet]
const myArr = Array.from(mySet);
//数组转集合
const mySet2 = new Set([1,2,3,4]);
//求交集 (ilter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素)
const intersection = new Set([...mySet].filter(x => mySet2.has(x)));
//求差集
const difference = new Set([...mySet].filter(x => !mySet2.has(x)));
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
扩展知识--蜂鸣器实验 2022-09-18 23:12:27