const s = new Set();
console.log(s);
[2,8,3,4,5,6].forEach((data) => {
s.add(data);
})
for(let i of s){
console.log(i);
}
//
var set = new Set([2,3,4,5,6,7]);
console.log([...set]);
console.log(set.size);
//
console.log('******')
let set1 = new Set();
var a = NaN;
var b = NaN;
set1.add(a);
set1.add(b);
console.log(set1.size);
console.log('******')
let set2 = new Set();
set2.add({});
console.log(set2.size);
console.log('******')
let sets = new Set([1,2,3,4,5,6,9]);
console.log(sets);
console.log(Array.from(sets));
//数组去重
function dedupe(array){
return Array.from(new Set(array));
}
console.log(dedupe([1,1,2,3,4,5,5,5,8,9]));
console.log('******')
//set遍历
let set3 = new Set(['red','blue','yellow']);
for(let item of set3.keys()){
console.log(item);
}
for(let item of set3.values()){
console.log(item);
}
for(let item of set3.entries()){
console.log(item);
}
for(let item of set3){
console.log(item);
}
console.log('******')
//forEach()
let set4 = new Set([1,2,3]);
set4.forEach((value, i) => {
console.log(value);
})
console.log('******')
let set5 = new Set([2,3,4,5,6]);
let arr1 = [...set5];
console.log(arr1);
console.log('******')
//并集(Union)、交集(Intersect)和差集(Difference)
let arr2 = new Set([2,3,4,5]);
let arr3 = new Set([3,4,5,6]);
//并集(Union
let newArr1 = new Set([...arr2,...arr3]);
console.log([...newArr1]);
//交集(Intersect)
let nweArr2 = new Set([...arr2].filter(x => arr3.has(x)));
console.log([...nweArr2]);
//差集(Difference)
let newArr3 = new Set([...arr2].filter(x => !arr3.has(x)));
console.log([...newArr3]);
console.log('******')
//Map结构的目的和基本用法
let m = new Map();
let o = {a:1};
m.set(o,'content');
console.log(m.get(o));
console.log(m.has(o));
m.delete(o);
console.log(m.has(o));
console.log('******')
var map = new Map([
['name', 'li'],
['age', 18]
]);
console.log(map.size);
console.log(map.has('name'));
console.log(map.get('name'));
console.log(map.get('12312ddd'))
console.log('******')
//遍历
let map1 = new Map([
['a',1],
['b',2],
['c',3]
]);
for(let x of map1.keys()){
console.log(x);
}
for(let x of map1.values()){
console.log(x);
}
for(let x of map1.entries()){
console.log(x[0],x[1]);
}
for(let [key,value] of map){
console.log(key,value);
}
console.log([...map1]);
console.log([...map1.keys()]);
console