1. 什么是set
数组是一系列无序,没有重复值的数据集合
2.Set 实例的方法和属性
一.方法:
1.add 方法:是给set 中添加成员的
const s=new Set(); a.add(1).add(2).add(2); console.log(s) //{1,2}
2.has 方法:判断里面是否有某个成员
const s=new Set(); a.add(1).add(2).add(2); console.log(s.has(1)) //true console.log(s.has(3)) //false
3.delete : 删除指定的成员
const s=new Set(); a.add(1).add(2).add(2); s.delete(1);
4.clear : 一键全部删除
5.forEach 方法:遍历访问set 中某位成员的方式
3.Set 构造函数的参数
4.Set 的 应用
1.数组去重
//1.数组去重 const s=new Set([1,2,1]); console.log(s); //[1,2] console.log([...s]);//1,2 //2.字符串去重 //‘abbachb’; const s=new Set('abbachb') console.log([...s].join())