es6实现并集 差集 交集

并集(俩个数组合并并且去重)

let arr1=[1,2,3,1,2,3,NaN,true,undefined,null];
   let arr2=[14,5,4,,3,NaN,true,NaN,null];
    function bj(ary1,ary2) {
        //合并,[...ary1,...ary2]
        //去重 new Set([...ary1,...ary2])
        //变成数组 [...new Set([...ary1,...ary2])];
        return [...new Set([...ary1,...ary2])];
    }
    bj(arr1,arr2);

交集(相同的部分)

function jj(ary1,ary2) {
    return ary1.filter((item)=>{
        return ary2.includes(item);
    })
}

console.log(jj(arr1, arr2));

差集() 去除并集中交集部分

function cj(ary1,ary2) {
    return bj(ary1,ary2).filter((item)=>{
        return !jj(ary1,ary2).includes(item);
    })
}

console.log(cj(arr1, arr2));

你可能感兴趣的:(ES6)