Set(交集、并集、差集)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Set(交集、并集、差集)</title>
</head>
<body>

</body>
</html>
<script type="text/javascript">

    var set1=new Set([1,2,1,2,1,2,true,NaN,true,"1","1"]);
    //得到一个去重后的类数组(类似数组的对象)
    //只有值value没有键key的对象
    console.log(set1);
    //size 个数
    //add
    console.log(set1.add(10));
    //has
    console.log(set1.has(1));
    //delete true/false
    console.log(set1.delete(1));;js知识点
    console.log(set1);
    //clear 没有返回值
    set1.clear();
    console.log(set1);

    var ary=[1,2,1,2,1,2,true,NaN,true,"1","1"];
    console.log([...new Set(ary)]);


    var ary1=[1,2,4,6,8];
    var ary2=[1,3,5,6,7,8];

    //并集
    function bingJi(ary1,ary2) {
        return [...new Set([...ary1,...ary2])].sort();
    }
    console.log(bingJi(ary1, ary2));



    //交集
    function jiaoJi(ary1,ary2) {
        return ary1.filter(function (item) {
            return ary2.includes(item)
        })
    }

    console.log(jiaoJi(ary1, ary2));
    //差集(并集-交集)

    function chaJi(ary1,ary2) {
        return bingJi(ary1,ary2).filter(function (item) {
            return !jiaoJi(ary1,ary2).includes(item);
        })
    }
    console.log(chaJi(ary1, ary2));

    var map=new Map([["a","a"],["b","b"]]);
    console.log(map);
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值