集合的实现3--ES6的set类型
来源:互联网 发布:2k17得分后卫身体数据 编辑:程序博客网 时间:2024/05/09 10:00
ES6也实现了Set类型。
ES6入门之set和map
我们看到
var set = new Set([1, 2, 2, 3, 3]);console.log(set);
看下面的注释部分。由于使用的是函数表达式(对应的是函数声明)。因此代码得放在合理的位置
/*使用数组来模拟集合*/function Set(arr) {var items = [];this.add = function (value) {if(this.has(value)) {return false;}items.push(value);return true;}this.has = function (value) {if(items.indexOf(value) == -1) {return false;}return true;}/*添加一些Set初始化的代码*/if(arr) {for(var i=0; i<arr.length; ++i) {this.add(arr[i])}}this.remove = function (value) {if(this.has(value)) {var index = items.indexOf(value);items.splice(index, 1);return true;}return false;}this.clear = function () {items = [];}this.size = function () {return items.length;}this.values = function () {return items;}this.print = function () {console.log(items);}this.union = function (other_set) {var new_set = new Set();var values = this.values();for(var i=0; i<values.length; ++i) {new_set.add(values[i]);}values = other_set.values();for(var i=0; i<values.length; ++i) {if(!new_set.has(values[i])) {new_set.add(values[i]);}}return new_set;}this.intersection = function (other_set) {var new_set = new Set();var values = this.values();for(var i=0; i<values.length; ++i) {if(other_set.has(values[i])) {new_set.add(values[i]);}}return new_set;}this.difference = function (other_set) {var new_set = new Set();var values = this.values();for(var i=0; i<values.length; ++i) {if(!other_set.has(values[i])) {new_set.add(values[i]);}}return new_set;}this.isSubset = function (other_set) {var flag = true;var values = other_set.values();for(var i=0; i<values.length; ++i) {if(!this.has(values[i])) {return false;}}return true;}}var set = new Set([1, 2, 2, 1, 3]);set.print();
阅读全文
0 0
- 集合的实现3--ES6的set类型
- Redis的集合类型(Set)
- Set集合的实现
- ES6的Set数据结构
- Java 7之集合类型第6篇 - Set集合的实现
- Java 7之集合类型第6篇 - Set集合的实现
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- vps+shadowsocks
- String.format用法
- 第一篇博文
- multiset的使用以及集合的运算
- YOLO9000在Mac下报Segement11错误的解决
- 集合的实现3--ES6的set类型
- HTML5 viewport模板
- react.js 显示隐藏效果
- spoj 1812(两个串的最长公共子串)后缀自动机
- git个人总结
- php获取当月天数及当月第一天及最后一天、上月第一天及最后一天实现方法
- C语言的灵魂--指针基本用法
- 流式编程
- POJ 2377 最大生成树(prim,kruskal)