function Set() {
var items = {};
/**
* 添加元素
* @param {[type]} value [description]
*/
this.add = function(value) {
if (!this.has(value)) {
items[value] = value;
return true;
}
return false;
};
/**
* 删除元素
* @param {[type]} value [description]
* @return {[type]} [description]
*/
this.remove = function(value) {
if (this.has(value)) {
delete items[value];
return true;
}
return false;
};
/**
* 判断元素是否存在集合里
* @param {[type]} value [description]
* @return {Boolean} [description]
*/
this.has = function(value) {
return items.hasOwnProperty(value);
};
/**
* 清空集合
* @return {[type]} [description]
*/
this.clear = function() {
items = {};
};
/**
* 获取集合的长度
* @return {[type]} [description]
*/
this.size = function() {
return Object.keys(items).length;
};
/**
* 获取集合的长度(兼容IE8)
* @return {[type]} [description]
*/
this.sizeLegacy = function() {
var count = 0;
for (var prop in items) {
if (items.hasOwnProperty(prop))
++count;
}
return count;
};
/**
* 获取集合
* @return {[type]} [description]
*/
this.values = function() {
return Object.keys(items);
};
/**
* 获取集合(兼容IE8)
* @return {[type]} [description]
*/
this.valuesLegacy = function() {
var keys = [];
for (var key in items) {
keys.push(key);
}
return keys;
};
/**
* 并集
* @param {[type]} otherSet [description]
* @return {[type]} [description]
*/
this.union = function(otherSet) {
var unionSet = new Set(); var values = this.valuesLegacy();
for (var i = 0; i < values.length; i++) {
unionSet.add(values[i]);
} values = otherSet.valuesLegacy();
for (var i = 0; i < values.length; i++) {
unionSet.add(values[i]);
}
return unionSet;
};
/**
* 交集
* @param {[type]} otherSet [description]
* @return {[type]} [description]
*/
this.intersection = function(otherSet) {
var intersectionSet = new Set(); var values = this.valuesLegacy();
for (var i = 0; i < values.length; i++) {
if (otherSet.has(values[i])) {
intersectionSet.add(values[i]);
}
} return intersectionSet;
};
/**
* 差集
* @param {[type]} otherSet [description]
* @return {[type]} [description]
*/
this.difference = function(otherSet) {
var differenceSet = new Set(); var values = this.valuesLegacy();
for (var i = 0; i < values.length; i++) {
if (!otherSet.has(values[i]))
differenceSet.add(values[i]);
}
return differenceSet;
};
/**
* 子集
* @param {[type]} otherSet [description]
* @return {[type]} [description]
*/
this.subset = function(otherSet) {
if (this.sizeLegacy() > otherSet.sizeLegacy()) {
return false;
} else {
var values = this.valuesLegacy();
for (var i = 0; i < values.length; i++) {
if (!otherSet.has(values[i])) {
return false;
}
}
return true;
}
}
} var set = new Set();
set.add(1);
set.add(2);
set.add(3); var set1 = new Set();
set1.add(3);
set1.add(4);
set1.add(5); var set2 = new Set();
set2.add(3); var unionSet = set.union(set1);
console.log(unionSet.valuesLegacy()); var intersectionSet = set.intersection(set1);
console.log(intersectionSet.valuesLegacy()); var differenceSet = set.difference(set1);
console.log(differenceSet.valuesLegacy()); console.log(set.subset(set1));
console.log(set2.subset(set));

记录Javascript集合操作的更多相关文章

  1. javascript集合求交集

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

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

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

  3. JavaScript常见集合操作

    JavaScript常见集合操作 集合的遍历 FOR循环(效率最高) 优点:JavaScript最普遍的for循环,执行效率最高 缺点:无法遍历对象 for(let i=0;i<array.le ...

  4. javascript DOM 操作

    在javascript中,经常会需要操作DOM操作,在此记录一下学习到DOM操作的知识. 一.JavaScript DOM 操作 1.1.DOM概念 DOM :Document Object Mode ...

  5. JAVASE02-Unit05: 集合操作 —— 查找表

    Unit05: 集合操作 -- 查找表 使用该类测试自定义元素的集合排序 package day05; /** * 使用该类测试自定义元素的集合排序 * @author adminitartor * ...

  6. JavaScript 节点操作Dom属性和方法(转)

    JavaScript 节点操作Dom属性和方法   一些常用的dom属性和方法,列出来作为手册用. 属性:   1.Attributes 存储节点的属性列表(只读)   2.childNodes 存储 ...

  7. Javascript 文件操作(整理版)

    Javascript 文件操作 一.功能实现核心:FileSystemObject 对象 其实,要在Javascript中实现文件操作功能,主要就是依靠FileSystemobject对象.在详细介绍 ...

  8. 集合操作出现的ConcurrentModificationException(源码分析)

    摘要: 为了保证线程安全,在迭代器迭代的过程中,线程是不能对集合本身进行操作(修改,删除,增加)的,否则会抛出ConcurrentModificationException的异常. 示例: publi ...

  9. Oracle集合操作

    在Oracle中提供了三种类型的集合操作: 并(UNION).交(INTERSECT).差(MINUS) UNION:将多个查询的结果组合到一个查询结果之中,并去掉反复值 UNION ALL:将多个查 ...

随机推荐

  1. falkonry

    falkonry.com/ 2019 Top 100 AI companies in the world

  2. C#运算符的简单使用测试

    在代码中看到的代码中|=,有点不太理解故重新学习了下位运算符. 位运算符在 c# 中的测试用例 [TestMethod] public void TestMethod1() { var a = fal ...

  3. 配置中心框架IConfCenter

    本篇和大家分享的是一个简易配置中心框架IConfCenter,框架是利用空余时间写的,主要以配置文件+redis存储方式作为数据同步驱动,目前支持的配置文件格式有 .properties 和 .con ...

  4. scala获取某个时间间隔的时间

    原始 dataFrame : //获取前7天的时间long类型 def getDaytimeTime(day:Int): Long = { val cal = Calendar.getInstance ...

  5. 如何在ASP.NET Core程序启动时运行异步任务(1)

    原文:Running async tasks on app startup in ASP.NET Core (Part 1) 作者:Andrew Lock 译者:Lamond Lu 背景 当我们做项目 ...

  6. 【朝花夕拾】Android性能篇之(七)Android跨进程通信篇

    前言 只要是面试高级工程师岗位,Android跨进程通信就是最受面试官青睐的知识点之一.Android系统的运行由大量相互独立的进程相互协助来完成的,所以Android进程间通信问题,是做好Andro ...

  7. 解决mac上每次升级nodejs都要重新安装扩展包的问题

    虽然有了一些新生派竞品比如yarn,但使用或者习惯了npm的开发者仍然大有人在. 以前用起来没注意到这个现象,最近一段时间发现,每次随着使用brew upgrade自动升级了nodejs版本,原来安装 ...

  8. HandlerInterceptor拦截实现对PathVariable变量的读取

    Http请求拦截作用 拦截后可以修改请求体 拦截后可以作一些其它统一的操作 问题提出 对于很多时间需要拦截很多Http请求,然后去获取一些参数,这些参数可能是querystring串,也可能是路由上的 ...

  9. IT技术团队管理之成长

    ------------------------------------------------------------------ 今天先到这儿,希望对您技术领导力, 企业管理,系统架构设计与评估, ...

  10. QT5.6.0 VS2013 Win764位系统QT环境搭建过程

    QT5.6.0 VS2013 Win764位系统QT环境搭建过程 没用过QT自己跟同事要了安装包,按照同事指导方法操作安装部署开发环境结果遇到好多问题,错误网上搜遍了所有帖子也没有找到合适的解决方案. ...