可能新的ECMA规范里已有了这些的实现,

但能自己从头开始实现,感觉也非常不错的哟。。。

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());
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());

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());

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());

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));
console.log(setA.subset(setC));

javascript集合的交,并,补,子集的操作实现的更多相关文章

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

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...

  2. python集合set{ }、集合函数及集合的交、差、并

    通过大括号括起来,用逗号分隔元素,特点 1.由不同元素组成,如果定义时存在相同元素,处理时会自动去重 2.无序 3.元素只能是不可变类型,即数字.字符串.布尔和元组,但集合本身可变 4.可直接定义集合 ...

  3. javascript集合求交集

    两集合求交集 思路: 1. 每一次从B数组中取一值,然后在A数组里逐个比较,如果有相等的,则保存.该算法复杂度为 O(MN). M, N 分别为数组 A B 的长度. 2. 因为A B 都排过序,所以 ...

  4. 【m元素集合的n个元素子集】

    /* m元素集合的n个元素子集 说明: 假设有个集合拥有m个元素,任意的从集合中取出n个元素,则这n个元素所形成的可能子集有那些? 解法: 假设有5个元素的集点,取出3个元素的可能子集如下: {1 2 ...

  5. [java] 求2个集合的交 差 并集

    要求2个集合的交 差 并集. set集合,如下 import java.util.HashSet; import java.util.Set; public class SetTest { publi ...

  6. 集合运算 - Java实现集合的交、并、差

    1.使用java的Set实现集合的交.并.差 package com.lfy.Set; import java.util.HashSet; import java.util.Set; /** * 集合 ...

  7. 【JavaScript实用技巧(二)】Js操作DOM(由问题引发的文章改版,新人大佬都可)

    [JavaScript实用技巧(二)]Js操作DOM(由问题引发的文章改版,新人大佬都可!) 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人 ...

  8. C# 集合已修改;可能无法执行枚举操作

    在winform 项目时遇到: 集合已修改;可能无法执行枚举操作的问题 错误原因:当用foreach遍历Collection时,如果对Collection有Add或者Remove或其他类似操作都会有这 ...

  9. Collection was modified; enumeration operation may not execute Dictionary 集合已修改;可能无法执行枚举操作

    public void ForeachDic() { Dictionary dic = new Dictionary(); dic.Add("1", 10); dic.Add(&q ...

随机推荐

  1. 用多态来实现U盘,Mp3,移动硬盘和电脑的对接,读取写入数据。

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 操作haproxy配置文件教师版

    作用: 可查,可增,可删,可修改 #_*_coding:utf-8_*_ import os def file_handle(filename,backend_data,record_list=Non ...

  3. Visual Studio Online Integrations-Build and release

                                      原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-dire ...

  4. C++中的异常处理(二)

    C++中的异常处理(二) 标签: c++C++异常处理 2012-11-24 20:56 1713人阅读 评论(2) 收藏 举报  分类: C++编程语言(24)  版权声明:本文为博主原创文章,未经 ...

  5. sudo: unable to resolve host ubuntu提示的解决

    http://blog.sina.com.cn/s/blog_6c9d65a1010180mg.html

  6. Session超时处理

    1.web.xml 添加配置: <!-- session超时 --> <filter> <filter-name>sessionFilter</filter- ...

  7. javascript常用排序算法总结

    算法是程序的灵魂.虽然在前端的开发环境中排序算法不是很经常用到,但常见的排序算法还是应该要掌握的.我在这里从网上整理了一下常见排序算法的javascript实现,方便以后查阅. 归并排序: 1 fun ...

  8. POJ 3274 Gold Balanced Lineup

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...

  9. 微信时代,"邮"你选择 腾讯企业邮箱推新玩法

    近日,腾讯企业邮箱在广州.北京.南京三地举办<微信时代,“邮”你选择>企业邮箱新方向客户见面会,同时也正式宣布将打通微信.“拥抱”移动办公,领航国内办公工具移动之“变”. 据了解,腾讯企业 ...

  10. Largest Rectangle in a Histogram

    2107: Largest Rectangle in a Histogram Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 777  Solved: 22 ...