最近在看阮一峰老师的
在看到Set数据结构实现数组的交集,并集还有差集,但是阮一峰老师实现差集貌似有点问题,特地来做下笔记:
const a = {fn: 1};const set = new Set([1,1,2,2,3,4,5,5,5,a,'a']);const b = new Set([6,7,8,9,5,4,3,'a','v']);// 并集const union = new Set([...set, ...b]);// 交集const intersect = new Set([...set].filter(x => b.has(x)));// 差集const difference = new Set([...union].filter(x => (!set.has(x) || !b.has(x))));
最后这条代码才能求到正确的差集
原文地址: