es6 Set的幾種使用場景

// 數組去重
    let arr = [1, 1, 2, 3];
    let unique = [... new Set(arr)];


    let a = new Set([1, 2, 3]);
    let b = new Set([4, 3, 2]);
    
    // 並集
    let union = [...new Set([...a, ...b])];
    
    // 交集
    let intersect = [...new Set([...a].filter(x => b.has(x)))];
    
    // 差集
    let difference = Array.from(new Set([...a].filter(x => !b.has(x))));
相關文章
相關標籤/搜索