Set集合是一种无重复元素的列表
let s = new Set();
let s1 = new Set([1,2,3,4,5]);
console.log(s1.size);
s1.add('55');
s1.delete(1);
console.log(s1.has('55'))
s1.clear();
for(let v of s1){
console.log(v);
}
let arr = [1,2,3,4,5,4,3,2,1];
// let result = new Set(arr) //不是一个数组,可以通过扩展运算符变成数组
let result = [...new Set(arr)];
console.log(result);
let arr = [1,2,3,4,5,4,3,2,1];
let arr2 = [4,5,6,5,6];
let result = [...new Set(arr)].filter(item=>{
let s2 = new Set(arr2); // 4,5,6
if(s2.has(item)){
return true;
}else{
return false;
}
})
console.log(result)
let arr = [1,2,3,4,5,4,3,2,1];
let arr2 = [4,5,6,5,6];
let union = [...new Set([...arr,...arr2])];
console.log(union);
let arr = [1,2,3,4,5,4,3,2,1];
let arr2 = [4,5,6,5,6];
let diff = [...new Set(arr)].filter(item=> !(new Set(arr2).has(item)))
console.log(diff);
Map集合内含有多组键值对,集合中每个元素分别存放着可以访问的键名和它对应的值
键名的等价性是通过调用Object.is()方法实现的,所以数字5和字符串5会被识别为两种类型,可以分别作为独立的两个键出现在程序中。
let m = new Map();
m.set('name','galaxy');
m.set('change',function(){
console.log('我们可以改变你')
})
console.log(m.size);
m.delete('name');
console.log(m.get('change'));
m.clear()
for(let v of m){
console.log(v);
}