let arr = [1, 2, 3, 4, 5, 4, 3, 2, 1]; //数组去重 let result = [...new Set(arr)]; console.log(result); //交集 let arr2 = [4, 5, 5, 6, 6]; let result = [...new Set(arr)].filter(item => { let s2 = new Set(arr2); if (s2.has(item)) { return true; } else { return false; } }) //并集 let union = [...new Set([...arr, ...arr2])]; console.log(union); // 差集 交集取反 let diff = [...new Set(arr).filter(item => { !(new Set(arr2).has(item)); })] console.log(diff);
标签:ES6,arr,set,...,item,Set,let,集合,new From: https://www.cnblogs.com/xiaobaizitaibai/p/16823937.html