JavaScript --- 集合
来源:互联网 发布:java实现序列化的代码 编辑:程序博客网 时间:2024/05/17 04:08
1、什么是集合?
集合是由一组无序且唯一(不能重复)的项组成的,这个数据结构使用了与有限集合相同的数学概念,但应用在计算机科学的数据结构中;以下内容主要以来实现一个ECMAScript 6中Set类的实现为基础。
2、实现我们的Set集合(MySet):
function MySet(){ var items = {};//这里使用的是对象,而不是数组 this.has = function(value){ return items.hasOwnProperty(value);//return value in items; }; this.add = function(value){ if(!items.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(){ return Object.keys(items).length; }; this.sizeLegacy = function(){ var count = 0; for(var porp in items){ if(items.hasOwnProperty(prop)){ ++count; } } return count; }; this.values = function(){ var keys = []; for(var key in items) { if(items.hasOwnProperty(key)){ keys.push(key); } } return keys; }; this.union = function(otherSet){ var unionSet = new MySet(); //第一个集合 var values = this.values(); for(var i=0;i<values.length;i++){ unionSet.add(values[i]); } //第二个集合 values = otherSet.values(); for(var i=0;i<values.length;i++) { unionSet.add(values[i]); } return unionSet; }; //交集 this.intersection = function(otherSet){ var intersectionSet = new MySet(); //当前集合 var values = this.values(); for(var i=0;i<values.length;i++) { if(otherSet.has(values[i])){//判断当前值在第二个集合中是否存在 intersectionSet.add(values[i]);//添加 } } return intersectionSet; }; //差集 this.differennce = function(otherSet){ var differennce = new MySet(); //当前集合 var values = this.values(); for(var i=0;i<values.length;i++){ if(!otherSet.has(values[i])){ differennce.add(values[i]); } } return differennce; }; //子集 this.subset = function(otherSet){ if(this.size() > otherSet.size())//子集不能比包含它的集合大 { return false; } var values = this.values(); for(var i=0;i<values.length;i++) { if(!otherSet.has(values[i])) { return false; } return true; } };}
3、使用我们实现的MySet集合,并简单介绍集合中方法:
//add方法主要用于向集合中添加元素,并且保证元素在集合中的唯一性var set = new MySet();set.add(1);set.add(2);console.log(set.itmes);//Object {1:1,2:2}//values 以数组的形式输出集合中的所有元素//has 判断一个元素在集合中是否存在(存在返回true,反之返回false)//size 返回当前集合的大小//remove 在集合中移除指定的元素var set = new MySet();set.add(1);console.log(set.values());//["1"]console.log(set.has(1));//trueconsole.log(set.size());//1set.add(2);console.log(set.values());//["1","2"]console.log(set.has(2));//trueconsole.log(set.size());//2set.remove(1);console.log(set.values());//["2"]set.remove(2);console.log(set.values());//[]//union 并集,合并集合A(setA)和集合B(setB)中的元素var setA = new MySet();//集合AsetA.add(1);setA.add(2);setA.add(3);var setB = new MySet();//集合BsetB.add(3);setB.add(4);setB.add(5);setB.add(6);var unionAB = setA.union(setB);console.log(unionAB);//["1","2","3","4","5","6"]//intersection 交集,取出集合A(setA)和集合B(setB)中的共同元素var setA = new MySet();//集合AsetA.add(1);setA.add(2);setA.add(3);var setB = new MySet();//集合BsetB.add(2);setB.add(3);setB.add(4);var intersectionAB = setA.intersection(setB);console.log(intersectionAB);//["2","3"]//differennce 差集,取值集合A(setA)的差集(在集合A中有出现的元素,但是在集合B中不存在的元素)var setA = new MySet();//集合AsetA.add(1);setA.add(2);setA.add(3);var setB = new MySet();//集合BsetB.add(2);setB.add(3);setB.add(4);var differennceAB = setA.differennce(setB);console.log(differennceAB);//["1"]//subset 子集,判断集合A是否是集合B的子集(因为集合B中包含了集合A中的所有元素,所以是属于子集,返回true)var setA = new MySet();//集合AsetA.add(1);setA.add(2);var setB = new MySet();//集合BsetB.add(1);setB.add(2);setB.add(3);var subsetAB = setA.subset(setB);console.log(subsetAB);//true
阅读全文
1 0
- javascript 集合
- javascript集合
- JavaScript --- 集合
- Javascript常用集合(1)
- javascript函数库:集合框架
- javaScript 脚本集合(搜集)
- javascript 实现hashtable集合
- javaScript技巧集合
- ERP JS(集合):javaScript
- JavaScript问题集合
- JavaScript使用技术集合
- javascript 问题集合
- JavaScript技巧集合
- javaScript技巧集合
- javaScript技巧集合
- javaScript技巧集合
- javascript技巧集合
- javascript技巧集合
- 云计算免费视频教程:Bashshell脚本编程详解
- 低功耗蓝牙开发知识:服务名称及ID
- 【沉淀】一张表的设计优化节省了两百万_客户不断盛誉……_这背后他究竟做对了什么?——记访谈汪建明
- 156个Python网络爬虫资源
- Windows中杀死占用某个端口的进程
- JavaScript --- 集合
- linux查找文件命令
- ASP.NET MVC 5
- Qt中OpenGL窗口创建的几种形式
- 安装PyQt5时缺少designer.exe的解决办法
- Android Activity 完全解析(下)
- 编写接口文档apidoc用法
- 路径规划算法
- jQuery+css+js+html5实现订座功能(仿电影票订座和火车票订座功能)