- 关于集合 :
集合通常由一组 无序 的,不能重复的元素 构成。集合比较常见的实现方式是哈希表。可以将集合看做特殊的数组。特殊之处在于里面的元素没有顺序,也不能重复。没有顺序意味着不能通过下标值进行访问,不能重复意味着相同的对象在集合中只能存在一份。学习集合,可以自己封装一个集合类。在 JS 中,2011年6月发布的 ES5 中包含了 Array 类。2015年6月发布的 ES6 中包含了 set (集合类),所以其实我们不用封装集合,可以直接使用它。
- 使用 JS 中 Object 类,封装集合 :
// 封装集合类
function Set(){
// 属性
this.items = {}
// 方法
//1,向集合添加一个新的项
Set.prototype.add = function(value){
if(this.has()){// 元素已存在,添加失败
return false;
}else{
// 元素添加成功
this.items[value] = value;
return true;
}
}
//2,从集合移除一个值
Set.prototype.remove = function(value){
// 判断是否包含这个元素
if(this.has(value)){
delete this.items[value];
return true;
}else{
return false;
}
}
//3,判断某个值是否在集合中
Set.prototype.has = function(value){
return this.items.hasOwnProperty(value);
}
//4,清空集合
Set.prototype.clear = function(){
this.items = {};
}
//5,集合中元素个数
Set.prototype.size = function(){
return Object.keys(this.items).length;
}
//6,返回一个包含集合中所有值的数组
Set.prototype.values = function(){
return Object.keys(this.items);
}
// ====================== 集合间的操作 =====================
// ----------------并集--------------
Set.prototype.union = function(otherSet){
// this:集合A;otherSet:集合B
// 新建一个集合
var unionSet = new Set();
// 把 集合 this.items 元素添加到新的集合
var values = this.values();
for(var i=0; i<values.length; i++){
unionSet.add(values[i]);
}
// 把 集合 otherSet 元素添加到新集合(添加操作,做了重复过滤,可以直接添加)
var values1 = otherSet.values();
for(var i=0; i<values1.length; i++){
unionSet.add(values1[i]);
}
return unionSet;
}
//-----------------交集---------------
Set.prototype.intersection = function(otherSet){
// this:集合A;otherSet:集合B
// 新建集合
var intersection = new Set();
// 从集合A中取出一个个元素,判断是否存在于集合B中
var values = this.values();
for(var i=0; i<values.length; i++){
if(otherSet.has(values[i])){
intersection.add(values[i])
}
}
// 返回交集结果
return intersection;
}
//-----------------差集---------------
Set.prototype.difference = function(otherSet){
// this:集合A;otherSet:集合B
// 创建新的集合
var difference = new Set();
// 遍历集合A,判断是否在集合B中,如果不在则将对应元素添加到新集合中
var values = this.values();
for(var i=0; i<values.length; i++){
if(!otherSet.has(values[i])){
difference.add(values[i]);
}
}
// 返回结果集合
return difference;
}
//-----------------子集--------------
Set.prototype.subset = function(otherSet){
// this:集合A;otherSet:集合B (判断集合 A 是否为集合 B 的子集)
// 遍历集合A中所有的元素,如果A中元素不存在于B中,则false;反之true
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.prototype.union = function(otherSet){
// this:集合A;otherSet:集合B
// 新建一个集合
var unionSet = new Set();
// 把 集合 this.items 元素添加到新的集合
var values = this.values();
for(var i=0; i<values.length; i++){
unionSet.add(values[i]);
}
// 把 集合 otherSet 元素添加到新集合(添加操作,做了重复过滤,可以直接添加)
var values1 = otherSet.values();
for(var i=0; i<values1.length; i++){
unionSet.add(values1[i]);
}
return unionSet;
}
- 交集:对于给定的两个集合,返回一个包含两个集合中公共元素的新集合。
Set.prototype.intersection = function(otherSet){
// this:集合A;otherSet:集合B
// 新建集合
var intersection = new Set();
// 从集合A中取出一个个元素,判断是否存在于集合B中
var values = this.values();
for(var i=0; i<values.length; i++){
if(otherSet.has(values[i])){
intersection.add(values[i])
}
}
// 返回交集结果
return intersection;
}
- 差集:对于两个给定的集合,返回一个包含所有存在于第一个集合且不存在于第二个集合的元素的新集合。
Set.prototype.difference = function(otherSet){
// this:集合A;otherSet:集合B
// 创建新的集合
var difference = new Set();
// 遍历集合A,判断是否在集合B中,如果不在则将对应元素添加到新集合中
var values = this.values();
for(var i=0; i<values.length; i++){
if(!otherSet.has(values[i])){
difference.add(values[i]);
}
}
// 返回结果集合
return difference;
}
- 子集:验证一个给定集合是否是另一个集合的子集。
Set.prototype.subset = function(otherSet){
// this:集合A;otherSet:集合B (判断集合 A 是否为集合 B 的子集)
// 遍历集合A中所有的元素,如果A中元素不存在于B中,则false;反之true
var values = this.values();
for(var i=0; i<values.length; i++){
if(!otherSet.has(values[i])){
return false;
}
}
return true;
}
- 关于字典 :
数组,集合,字典是几乎编程语言都会默认提供的数据类型。在 JS 中默认提供数组,在 ES6 中增加了集合和字典。字典 的特点:主要特点是一一对应的关系,字典中的 key 是不可以重复的,而 value 可以重复,并且字典中的key是无序的。通过 key 取出 value 值。有些编程语言称 key——value 他们之间的这种映射关系为字典,而在有些编程语言中被称为 Map。