当前位置:首页 > 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 的操作。

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 对象,可以直接使用。

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 的功能,实现集合的常见操作。

js 实现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实现轮播

实现基础轮播效果 使用HTML结构创建轮播容器和图片元素: <div class="carousel"> <div class="carousel-inner">…

js实现正交

js实现正交

正交的概念 正交在数学和计算机科学中通常指两个向量垂直或线性无关。在编程中,正交性常被用于设计模块化、低耦合的系统。 向量正交判断 判断两个向量是否正交可以通过点积是否为0来实现: fun…

js实现路由

js实现路由

js实现路由的方法 在JavaScript中实现路由功能可以通过多种方式完成,以下是几种常见的方法: 使用原生JavaScript实现路由 通过监听window.onhashchange事件来实现基…

js实现排序

js实现排序

数组排序方法 JavaScript提供了内置的sort()方法用于数组排序。默认情况下,sort()将元素转换为字符串并按照Unicode码点排序。对于数字排序,需传入比较函数。 const num…

js和jquery

js和jquery

JavaScript 与 jQuery 的关系与区别 JavaScript 是一种动态编程语言,主要用于网页开发,使网页具有交互性。它是浏览器的原生脚本语言,可以直接在浏览器中运行。 jQuery…

vue js 实现排序

vue js 实现排序

Vue.js 实现排序的方法 在 Vue.js 中实现排序可以通过多种方式完成,包括使用计算属性、方法或直接操作数据。以下是几种常见的实现方式: 使用计算属性排序 计算属性可以自动响应数据变化并重新…