当前位置:首页 > JavaScript

js 实现set

2026-03-02 06:46:16JavaScript

使用对象模拟 Set

在 JavaScript 中,可以通过对象来模拟 Set 的功能。对象的键是唯一的,可以用来存储 Set 的元素。

function CustomSet() {
  this.items = {};
}

CustomSet.prototype.add = function(value) {
  this.items[value] = true;
};

CustomSet.prototype.has = function(value) {
  return this.items.hasOwnProperty(value);
};

CustomSet.prototype.delete = function(value) {
  delete this.items[value];
};

CustomSet.prototype.clear = function() {
  this.items = {};
};

CustomSet.prototype.size = function() {
  return Object.keys(this.items).length;
};

使用数组实现 Set

另一种方法是使用数组来存储元素,并通过数组的方法来实现 Set 的操作。

js 实现set

function ArraySet() {
  this.items = [];
}

ArraySet.prototype.add = function(value) {
  if (!this.items.includes(value)) {
    this.items.push(value);
  }
};

ArraySet.prototype.has = function(value) {
  return this.items.includes(value);
};

ArraySet.prototype.delete = function(value) {
  const index = this.items.indexOf(value);
  if (index !== -1) {
    this.items.splice(index, 1);
    return true;
  }
  return false;
};

ArraySet.prototype.clear = function() {
  this.items = [];
};

ArraySet.prototype.size = function() {
  return this.items.length;
};

ES6 原生 Set

现代 JavaScript 已经内置了 Set 对象,可以直接使用。

js 实现set

const set = new Set();

set.add(1);
set.add(2);
set.add(1); // 重复值不会被添加

console.log(set.has(1)); // true
console.log(set.size); // 2

set.delete(1);
console.log(set.has(1)); // false

set.clear();
console.log(set.size); // 0

自定义 Set 的迭代功能

如果需要自定义 Set 的迭代功能,可以实现 Symbol.iterator 方法。

CustomSet.prototype[Symbol.iterator] = function*() {
  for (const key in this.items) {
    yield key;
  }
};

const customSet = new CustomSet();
customSet.add('a');
customSet.add('b');

for (const item of customSet) {
  console.log(item); // 'a', 'b'
}

实现 Set 的并集、交集和差集

可以扩展 Set 的功能,实现集合的常见操作。

CustomSet.prototype.union = function(otherSet) {
  const unionSet = new CustomSet();
  for (const item in this.items) {
    unionSet.add(item);
  }
  for (const item in otherSet.items) {
    unionSet.add(item);
  }
  return unionSet;
};

CustomSet.prototype.intersection = function(otherSet) {
  const intersectionSet = new CustomSet();
  for (const item in this.items) {
    if (otherSet.has(item)) {
      intersectionSet.add(item);
    }
  }
  return intersectionSet;
};

CustomSet.prototype.difference = function(otherSet) {
  const differenceSet = new CustomSet();
  for (const item in this.items) {
    if (!otherSet.has(item)) {
      differenceSet.add(item);
    }
  }
  return differenceSet;
};

标签: jsset
分享给朋友:

相关文章

js实现验证码

js实现验证码

实现验证码的JavaScript方法 生成随机验证码 使用Math.random()生成随机字符串,结合数字和字母: function generateCaptcha() { const cha…

js实现动画

js实现动画

使用 CSS 动画与 JavaScript 控制 通过 JavaScript 动态添加或移除 CSS 类来触发动画。CSS 定义关键帧(@keyframes),JavaScript 通过 classL…

js实现延迟

js实现延迟

实现延迟的方法 在JavaScript中,实现延迟操作有多种方式,以下是几种常见的方法: 使用setTimeout函数 setTimeout是JavaScript中最常用的延迟执行方法。它接受一个回…

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…

链表实现js

链表实现js

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针(单向链表)或两个指针(双向链表)。与数组不同,链表在内存中非连续存储,插入和删除操作更高效,但随机…

js 实现全屏

js 实现全屏

使用 requestFullscreen 方法 通过调用元素的 requestFullscreen 方法可以实现全屏。该方法兼容现代浏览器,但不同浏览器可能需要前缀。 const element =…