JS實現並集,交集和差集

看風景就發表於2017-08-12

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

相關文章