使用Set进行数组去重,求交并差

         // 数组去重
        let arr = [1, 1, 2, 3, 5, 8]
        let uniq = [...new Set(arr)]

        // 求两数组的交并差
        let aArr = [1, 2, 3], bArr = [2, 3, 4]
        let unioin = [...new Set([...aArr, ...bArr])]
        console.log(unioin) // [1, 2, 3, 4]
        let intersect = aArr.filter(x => (new Set(bArr)).has(x))
        console.log(intersect) // [2, 3]
        let difference = aArr.filter(x => !(new Set(bArr)).has(x))
        console.log(difference) // [1]    

 

posted on 2019-03-10 16:36  时光游弋  阅读(330)  评论(0编辑  收藏  举报