Set之数组的并、交、差
2018-05-28 本文已影响0人
樱木夜访流川枫
1、并集:先合并再set
var a = [1,2];
var b = [2,3];
var set1 = new Set(a.concat(b)); // {1,2,3}
var set2 = new Set([...a,...b]); // {1,2,3}
2、交集:先转set,在转数组,利用数组的filter、set的has
var arr1 = [1,2];
var arr2 = [2,3];
var set1 = new Set(arr1);
var set2 = new Set(arr2);
var arr = [...set1].filter(item => {return set2.has(item)})
差集:
var arr = [...set1].filter(item => {return !set2.has(item)})