new Set()
Set 对象存储的值总是唯一的,可以用来去重数组。
Set 对象作用
1.数组去重
var arr = [1,2,3,3,1,4]; [...new Set(arr)]; // [1, 2, 3, 4] Array.from(new Set(arr)); // [1, 2, 3, 4] [...new Set('ababbc')].join(''); // "abc" 字符串去重 new Set('ice doughnut'); //Set(11) {"i", "c", "e", " ", "d", "o","u","g","h","n","u","t"}
2.并集
var a = new Set([1, 2, 3]); var b = new Set([4, 3, 2]); var union = new Set([...a, ...b]); // {1, 2, 3, 4}
3.交集
var a = new Set([1, 2, 3]); var b = new Set([4, 3, 2]); var intersect = new Set([...a].filter(x => b.has(x))); // {2, 3}
4.差集
var a = new Set([1, 2, 3]); var b = new Set([4, 3, 2]); var difference = new Set([...a].filter(x => !b.has(x))); // {1}
生前无需久睡,死后自会长眠,努力解决生活中遇到的各种问题,不畏将来,勇敢面对,加油,你是最胖的,哈哈哈!!!!!
加油