首页 > 代码库 > JS实现并集,交集和差集

JS实现并集,交集和差集

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

并集
let union = new Set([...set1, ...set2]);

交集
let intersect = new Set([...set1].filter( x => set2.has(x)));

差集
let difference = new Set([...set1].filter(x => !set2.has(x)));

 

 


出处:http://blog.csdn.net/haijing1995/article/details/72842810

JS实现并集,交集和差集