javascript集合的交,并,补,子集,长度,新增,删除,清空等操作

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>javascript集合的交,并,补,子集的操作实现</title>
    <script src="JS/jquery-easyui-1.5/jquery.min.js"></script>
    <script>
        //集合操作类
        function Set() {
            var items = {};

            //是否存在
            this.has = function (value) {
                return items.hasOwnProperty(value);
            };

            //添加记录
            this.add = function (value) {
                if (!this.has(value)) {
                    items[value] = value;
                    return true;
                }
                return false;
            };

            //删除记录
            this.remove = function (value) {
                if (this.has(value)) {
                    delete items[value];
                    return true;
                }
                return false;
            };

            //清空数组
            this.clear = function () {
                items = {};
            };

            //数组长度
            this.size = function () {
                var count = 0;
                for (var prop in items) {
                    if (items.hasOwnProperty(prop)) {
                        ++count;
                    }
                }
                return count;
            };

            //数组
            this.values = function () {
                var keys = [];
                for (var key in items) {
                    keys.push(key);
                }
                return keys;
            };

            //合集
            this.union = function (otherSet) {
                var unionSet = new Set();

                var values = this.values();
                for (var i = 0; i < values.length; i++) {
                    unionSet.add(values[i]);
                }

                var values = otherSet.values();
                for (var i = 0; i < values.length; i++) {
                    unionSet.add(values[i]);
                }
                return unionSet;
            };

            //交集
            this.intersection = function (otherSet) {
                var intersectionSet = new Set();

                var values = this.values();
                for (var i = 0; i < values.length; i++) {
                    if (otherSet.has(values[i])) {
                        intersectionSet.add(values[i]);
                    }
                }
                return intersectionSet;
            };

            //差集
            this.difference = function (otherSet) {
                var differenceSet = new Set();

                var values = this.values();
                for (var i = 0; i < values.length; i++) {
                    if (!otherSet.has(values[i])) {
                        differenceSet.add(values[i]);
                    }
                }
                return differenceSet;
            };

            //子集
            this.subset = function (otherSet) {
                if (this.size() > otherSet.size()) {
                    return false;
                } else {
                    var values = this.values();
                    for (var i = 0; i < values.length; i++) {
                        if (!otherSet.has(values[i])) {
                            return false;
                        }
                    }
                    return true;
                }
            }
        }

        var set = new Set();
        set.add(1);
        console.log(set.values()); //输出["1"]
        console.log(set.has(1)); //输出true
        console.log(set.size()); //输出1
        set.add(2);
        console.log(set.values()); //输出["1", "2"]
        console.log(set.has(2)); //true
        console.log(set.size()); //2
        set.remove(1);
        console.log(set.values()); //输出["2"]
        set.remove(2);
        console.log(set.values()); //输出[]

        var setA = new Set();
        setA.add(1);
        setA.add(2);
        setA.add(3);
        var setB = new Set();
        setB.add(3);
        setB.add(4);
        setB.add(5);
        setB.add(6);
        var unionAB = setA.union(setB);  //合集
        console.log(unionAB.values());  //输出 ["1", "2", "3", "4", "5", "6"]

        var setA = new Set();
        setA.add(1);
        setA.add(2);
        setA.add(3);
        var setB = new Set();
        setB.add(2);
        setB.add(3);
        setB.add(4);
        var intersectionAB = setA.intersection(setB); //交集
        console.log(intersectionAB.values());  //输出 ["2", "3"]

        var setA = new Set();
        setA.add(1);
        setA.add(2);
        setA.add(3);
        var setB = new Set();
        setB.add(2);
        setB.add(3);
        setB.add(4);
        var differenceAB = setA.difference(setB);  //差集
        console.log(differenceAB.values()); //输出 ["1"]

        var setA = new Set();
        setA.add(1);
        setA.add(2);
        var setB = new Set();
        setB.add(1);
        setB.add(2);
        setB.add(3);
        var setC = new Set();
        setC.add(2);
        setC.add(3);
        setC.add(4);
        console.log(setA.subset(setB));  //子集  true
        console.log(setA.subset(setC));  //子集  false
    </script>
</head>
<body>

    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值