集合的常用操作

先回顾一下ES6类的写法

class PersonName {
  constructor(name) {
    this.name = name;
  }
  sayName() {
    console.log(this.name);
  }
}
let person = new PersonName('jojo');
person.sayName();    // jojo

以及两个类之间的传值

class PersonClass {
  constructor(name) {
    this.name = name;
  }
}

class Teacher{
  constructor(data) {
    this.data = data
  }
  show(name){
    var person = new PersonClass(name);
    return person.name
  }
}

var teacher = new Teacher();
console.log(teacher.show('jojo'));

ES6中其实已经封装了 set 类
所以其实可以不用封装,直接使用它。
但是还是可以自己封装一个的

① 集合比较常见的实现方式是哈希表
// 1.集合里面元素没有顺序,也没有重复
// 2.不能通过下标值进行访问。

// 集合有以下常见的操作方法
1.add(value):向集合添加一个新的项
2.remove(value) 从集合移除一个值
3.has(value) 如果值在集合中,返回true
4.clear() 移除集合中所有项
5.size() 返回集合中包含元素的数量,与数组的length属性类似
6.values() 返回一个包含集合中所有值的数组

// ES6里面已经封装了set类,所以其实可以不用封装,直接使用它
class Set {
  // 创建items为一个空的类
  items = {}
  // 1. add方法;向集合添加一个新的项
  add(value){
    // 判断是否已经包含了这个元素
    if (this.has(value)){
      return false
    }

    this.items[value] = value;
    return true
  }

  // 2. has方法; 如果值在集合中,返回true
  has(value){
    return this.items.hasOwnProperty(value);
  }

  // 3. remove方法;从集合移除一个值
  remove(value){
    if (!this.has(value)) return false
    delete this.items[value]
    return true;
  }

  // 4. clear方法;移除集合中所有项
  clear(){
    this.items = {}
  }

  // 5. size 方法;返回集合中包含元素的数量,与数组的length属性类似
  size(){
    return Object.keys(this.items).length
  }

  // 6. values方法;返回一个包含集合中所有值的数组
  values(){
    return Object.keys(this.items)
  }
}

下面用以上基础操作实现,并集、交集、差集、判断子集的操作。

  • 1.求并集操作,一定先要明白this的作用
union(otherSet){
    // this:集合对象A,对象A就是我们一开始创建的,我们现在编写union操作就是在对象A中
    // 因此,可以用this
    // otherSet:集合对象B
    // 1.创建新的集合
    var  unionSet = new Set()
    // 2.将A集合中所有的元素添加到新的集合中
    var values = this.values();
    for (var i = 0;i<values.length;i++){
      unionSet.add(values[i])
    }
    // 3.取出B集合中的元素,判断是否需要添加到新集合
    // 重新赋值values
    values = otherSet.values()
    for (var i = 0;i<values.length;i++){
      unionSet.add(values[i])
    }
    return unionSet;
  }

2.求交集

intersection(otherSet){
    // this:集合A
    // otherSet:集合B
    // 1.创建新的集合
    var intersectionSet = new Set();
    // 2.从A中取出一个元素,判断是否同时存在于集合B中,存在新集合中
    var values = this.values();
    for (var i=0;i<values.length;i++){
      var item = values[i]
      if (otherSet.has(item)){
        intersectionSet.add(item)
      }
    }
    return intersectionSet
  }
  • 3.求差集
difference(otherSet){
    var differentSet = new Set();
    var values = this.values();
    for (var i=0;i<values.length;i++){
      var item = values[i];
      if (!otherSet.has(item)){
        differentSet.add(item);
      }
    }
    return differentSet;
  }
  • 4.判断是否是子集

解法1
先求交集,交集与两个集合对比,交集 = 其中一个集合的话,它就是子集。
// Javascript不能直接用==或者===来判断两个数组是否相等,
// 无论是相等还是全等都不行,以下两行JS代码都会返回false,因此比较两个字符串是否相等,在前面加个toString

subset(otherSet){
    var intersectionSet = this.intersection(otherSet)
    if (intersectionSet.values().toString() === this.values().toString())
      return true
    else
      return false
  }

解法2
集合A中的元素在集合B中都可以找得到,就返回true,否则就返回false

subset(otherSet){
   var values = this.values();
   for(var i=0;i<values.length;i++){
     var item = values[i];
     if (!otherSet.has(item)){
       return false;
     }
   }
   return true;intersectionSet
  }

测试代码

var set = new Set()
set.add('aaa');
set.add('bbb');
set.add('ccc');
set.add('ddd');
// 打印集合中的所有元素
console.log(set.values());
// 删除bbb元素
set.remove('bbb');
// 打印集合中的所有元素
console.log(set.values());
// 集合元素个数
console.log(set.size());

测试2

var setA = new Set();
setA.add('a1');
setA.add('a2');
setA.add('a3');
var setB = new Set();
setB.add('a1');
setB.add('a2');
// setB.add('b1');
console.log(setA.values());
console.log(setB.values());
// 返回setA,setB的并集
console.log(setA.union(setB).values());
console.log(setA.intersection(setB).values());
console.log(setA.difference(setB).values());
console.log(setB.subset(setA));
image.png

笔记:

  • hasOwnProperty() 方法会返回一个布尔值,
  • 指示对象自身属性中是否具有指定的属性(也就是,是否有指定的键)。
  • 注意查找的是键,不是值
const items = {};
items['111'] = 'aaa';
items['222'] = 'bbb';
items.property1 = 42;
console.log(items);
function has(value,items){
  // 判断是否含有本指定元素
  return items.hasOwnProperty(value);
}
// 注意查找的是键,不是值
console.log(has('111',items));
console.log(has('aaa',items));
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容