<!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>

本文转载:CSDN博客