JavaScript Set
2024-09-06 03:05:16
function Set() {
var items = {};
this.has = function(value) {
return value in items
}
this.add = function(value) {
if (!this.has(value)) {
items[value] = value;
return true
}
return false
}
this.remove = function() {
if (this.has(value)) {
delete items[value];
return true
}
return false
}
this.size = function() {
return Object.keys(items).length
}
this.values = function() {
return Object.keys(items)
}
this.union = function(otherSet) {
var unionSet = new Set();
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 intersection = new Set();
var values = this.values();
for (var i = 0; i < values.length; i++) {
if (otherSet.has(values[i])) {
intersection.add(values[i])
}
}
return intersection
}
this.difference = function(otherSet) {
var differece = new Set();
var values = this.values();
for (var i = 0; i < values.length; i++) {
if (!otherSet.has(values[i])) {
differece.add(values[i])
}
}
return differece
}
this.subSet = function(otherSet) {
var subSet = new Set();
if (this.size() > otherSet.size()) {
return false
}
var values = this.values();
for (var i = 0; values.length; i++) {
if (!otherSet.has(values[i])) {
return false
}
}
return true
}
}
最新文章
- Android编译过程中的碎碎念
- Javascript 中的严格模式
- 编写更好的jQuery代码
- 数据库SQL
- Lock较synchronized多出的特性
- 快乐的JS正则表达式(一)
- IE插件收集
- wrk 进程管理
- 五分钟打造自己的sql性能分析工具
- Csharp日常笔记
- <;转>;MySql 与Oracle区别
- .net web 点击链接在页面指定位置显示DIV的问题
- 国家语言,语言代码,locale id对应表
- Python之旅Day13 JavaScript与DOM部分
- Java实现三大简单排序算法
- Maven 学习 -- 目录
- 安恒7月赛wp
- Linux 各类设置、配置、使用技巧参考,Linux使用集锦
- SSH 报错解决方法记录汇总
- iOS 数据类型转换