封装一个Mymap和Myset

class MyMap {
  constructor() {
    this.dict = {};
    this.size = 0;
  }

  updateSize() {
    this.size = Object.keys(this.dict).length;
  }

  set(key, value) {
    this.dict[key] = value;
    this.updateSize();
  }

  delete(key) {
    delete this.dict[key];
    this.updateSize();
  }

  clear() {
    this.dict = {};
    this.size = 0;
  }

  get(key) {
    return this.dict[key];
  }

  has(key) {
    return Object.keys(this.dict).includes(key);
  }

  keys() {
    return Object.keys(this.dict);
  }

  values() {
    return Object.keys(this.dict).map((key) => this.dict[key]);
  }

  entries() {
    return Object.keys(this.dict).map((key) => [key, this.dict[key]]);
  }

  forEach(handler){
      for(let key in this.dict){
          handler.apply(this,[this.dict[key],key,this])
      }
  }
}

!(function () {
  const map = new MyMap();

  /* 增加数据 */
  map.set("six", "陆小凤");
  map.set("seven", "洪七公");
  map.set("eight", "袁八爷");

  /* 修改与增加一样 */
  map.set("seven", "白景琦");

  /* 删除数据 */
  //   map.delete("eight");
  //   map.clear()

  /* 查询数据 */
  console.log(map.get("six"));
  console.log(map.get("seven"));
  console.log(map.get("eight"));

  /* 更多查询 */
  console.log(map.size);

  /* 查询有无键值 */
  console.log(map.has("seven"));
  console.log(map.has("nine"));

  /* 查询所有的键或值 */
  console.log(map.keys());
  console.log(map.values());
  console.log(map.entries());

  /* 遍历迭代器(iterators) */
  for (const iterator of map.entries()) {
    console.log(iterator);
  }

  /* 遍历 */
  map.forEach(
      (value, key, m) => console.log(key, value)
  );

})();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
class MySet {
  constructor(arr = []) {
    this.arr = arr.filter((item, index) => arr.indexOf(item) === index);
    this.size = this.arr.length;
  }

  updateSize() {
    this.size = this.arr.length;
  }

  add(value) {
    if (!this.arr.includes(value)) {
      this.arr.push(value);
      this.updateSize();
    }
  }

  delete(value) {
    for (let i = 0; i < this.arr.length; i++) {
      if (this.arr[i] === value) {
        this.arr.splice(i, 1);
        this.updateSize();
        break;
      }
    }
  }

  clear() {
    this.arr = [];
    this.updateSize();
  }

  has(value) {
    return this.arr.find((item) => item === value) !== undefined;
  }

  forEach(handler) {
    for (let i = 0; i < this.arr.length; i++) {
      handler.apply(this, [this.arr[i], i, this]);
    }
  }

  keys() {
    return this.arr;
  }

  values() {
    return this.arr;
  }

  entries() {
    return this.arr.map((item) => ({ [item]: item }));
  }

  toArray() {
    return this.arr;
  }
}

!(function () {
  // const set = new Set()
  const set = new MySet([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8]);
  console.log(set.size);

  /* 数据已经实现了去重 */

  /* CRUD */
  set.add(10);
  set.add(10);
  set.delete(5);
  // set.clear()

  console.log(set.has(3));
  console.log(set.has(11));

  set.forEach((value, index) => console.log(value, index));

  console.log(set.keys());
  console.log(set.values());
  console.log(set.entries());
  for (const iterator of set.values()) {
    console.log(iterator);
  }
})();
 
posted @ 2022-08-08 22:46  禅心佛子  阅读(45)  评论(0)    收藏  举报