可能新的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. rockmongo用法

    .简单查询 //xid=560870 and type=video { , "type": "video" } //查询数组中的数据 array( " ...

  2. Java多线程基础(一)

    一.基本概念 线程状态图包括五种状态 1.新建状态(New):线程对象被创建后,就进入新建状态.例如,Thread thread=new Thread(); 2.就绪状态(Runnable):也被称为 ...

  3. 自定义NSLog无时间

    #define SXLog(FORMAT, ...) fprintf(stderr,"file --\t%s\nline --\t%d\nmethd --\t%s\noutput --\t\ ...

  4. 数据库的模糊查询mybatis

    <!-- oracle --> <select id="searchUserBySearchName" parameterType="java.lang ...

  5. linux 中文件夹的文件按照时间倒序或者升序排列

    1,按照时间升序 命令:ls -lrt 详细解释: -l use a long listing format 以长列表方式显示(详细信息方式) -t sort by modification time ...

  6. Nginx HA 及https配置部署

    Nginx HA 整体方案架构为: (内网192.168.199.5) +-----------VIP----------+ | | | | Master Backup 192.168.199.90 ...

  7. Matlab图像处理入门

    1. Matlab基础 1.1     数据格式 Matlab默认的数据格式为双精度浮点数的矩阵或数组,同时支持其它数据类型.Matlab将单变量看作1´1的数组.Matlab支持的数据类型如下: 索 ...

  8. ruby的in?方法

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) $ irbirb(main):001:0> a = 1=> 1irb(main):002 ...

  9. users

    NAME users - print the user names of users currently logged in to the current host SYNOPSIS users [O ...

  10. microsoft office安装选择

    office分为零售版和批量授权版 零售版(文件名以cn开头)需要提供序列号才可以安装,而批量授权版(文件名以SW开头)可以先安装试用一段时间.