set 它类似于数组,但是成员的值都是唯一的,没有重复的值。
方法:add(),has(),delete(),clear()
转为对像:
const items = new Set([1, 2, 3, 4, 5]);
const array = Array.from(items);
去除数组重复成员:
function dedupe(array) {
return Array.from(new Set(array));
}
dedupe([1, 1, 2, 3]) // [1, 2, 3]
遍历:
const items = new Set([1, 2, 3, 4, 5]);
items.forEach((val)=>{
console.log(val)
})
数组操作:
let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);
// 并集
let union = new Set([...a, ...b]);
// Set {1, 2, 3, 4}
// 交集
let intersect = new Set([...a].filter(x => b.has(x)));
// set {2, 3}
// 差集
let difference = new Set([...a].filter(x => !b.has(x)));
// Set {1}
var m = new Map(); // 空Map
m.set('Adam', 67); // 添加新的key-value
m.set('Bob', 59);
m.has('Adam'); // 是否存在key 'Adam': true
m.get('Adam'); // 67
m.delete('Adam'); // 删除key 'Adam'
m.get('Adam'); // undefined
console.log(m)
var pa=[1,2,3,4,4,4]
var pb=new Set(pa)
pb.add(5,6,7,4)
console.log(pb)
Map