集合中的交集和差异

集合中没有用于交集和差异的内置方法,但你仍然可以实现该方法,但将它们转换为数组,过滤并转换回集合:

var set1 = new Set([1, 2, 3, 4]),
    set2 = new Set([3, 4, 5, 6]);

const intersection = new Set(Array.from(set1).filter(x => set2.has(x)));//Set {3, 4}
const difference = new Set(Array.from(set1).filter(x => !set2.has(x))); //Set {1, 2}