Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现es6的set集合 #21

Open
Sunny-117 opened this issue Nov 3, 2022 · 1 comment
Open

实现es6的set集合 #21

Sunny-117 opened this issue Nov 3, 2022 · 1 comment

Comments

@Sunny-117
Copy link
Owner

class MySet {
  constructor(iterator = []) {//不传默认空数组
    if (typeof iterator[Symbol.iterator] !== "function") {
      throw new TypeError(`你提供的${iterator}不是一个可迭代的对象`)
    }
    this._datas = [];
    for (const item of iterator) {
      this.add(item);
    }
  }

  get size() {
    return this._datas.length;
  }

  add(data) {
    if (!this.has(data)) {// 不包含data,才加入
      this._datas.push(data);
    }
  }

  has(data) {// 是否有data
    for (const item of this._datas) {
      if (this.isEqual(data, item)) {// isEqual判断两个数据是否相等
        return true;
      }
    }
    return false;
  }
  delete(data) {
    for (let i = 0; i < this._datas.length; i++) {
      const element = this._datas[i];
      if (this.isEqual(element, data)) {
        //删除
        this._datas.splice(i, 1);
        return true;
      }
    }
    return false;
  }

  clear() {
    this._datas.length = 0;
  }
  *[Symbol.iterator]() {// 遍历效果
    for (const item of this._datas) {
      yield item;
    }
  }
  forEach(callback) {
    for (const item of this._datas) {
      callback(item, item, this);
    }
  }
  /**
     * 判断两个数据是否相等
     * @param {*} data1 
     * @param {*} data2 
     */
  isEqual(data1, data2) {
    if (data1 === 0 && data2 === 0) {
      return true;
    }
    return Object.is(data1, data2);
  }
}
@kangkang123269
Copy link

class mySet {
    constructor(optionsArr = []) {
        if (typeof iterator[Symbol.iterator] !== "function") {
            throw new TypeError(`你提供的${iterator}不是一个可迭代的对象`)
        }
        this.list = {}
        this.size = optionsArr.length
        optionsArr.forEach(val => {
            this.list[val] = val
        })
    }
    add(val) {
        if (!this.list[val]) {
            this.list[val] = val
            this.size++
            return true
        }
        return false
    }
    has(val) {
        // 判断下是否是对象,如果是对象返回false
        if (val !== null && typeof val === 'object') return false
        return this.list.hasOwnProperty(val)
    }

    delete(val) {
        if (this.list[val]) {
            delete this.list[val]
            this.size--
            return true
        }
        return false
    }
    clear() {
        this.list = {}
        this.size = 0
    }
    keys() {
        // 键和值是相等的,以值为主
        return Object.values(this.list)
    }
    values() {
        // 键和值是相等的,以值为主
        return Object.values(this.list)
    }
    entries() {
        // 键和值是相等的,以值为主
        return Object.entries(this.list).map(item => {
            item[0] = item[1]
            return item
        })
    }
    forEach(callback) {
        let keys = this.keys()
        for (let i = 0; i < keys.length; i++) {
            callback(keys[i], keys[i], this)
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants