Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js hashset

作者:冷月魄   发布日期:2026-01-07   浏览:17

// 创建一个 HashSet 使用 JavaScript 的 Set 对象
const hashSet = new Set();

// 添加元素到 HashSet
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);

// 检查元素是否存在于 HashSet 中
console.log(hashSet.has(2)); // 输出: true

// 删除元素
hashSet.delete(2);

// 再次检查该元素是否存在
console.log(hashSet.has(2)); // 输出: false

// 获取 HashSet 的大小
console.log(hashSet.size); // 输出: 2

// 遍历 HashSet
hashSet.forEach(value => {
    console.log(value);
});

// 清空 HashSet
hashSet.clear();
console.log(hashSet.size); // 输出: 0

解释说明:

  • Set 是 ES6 引入的一种新的数据结构,类似于数组,但成员的值都是唯一的,没有重复的值。
  • add() 方法用于向集合中添加一个新元素。
  • has() 方法用于检查集合中是否存在某个元素,返回布尔值。
  • delete() 方法用于从集合中删除某个元素。
  • size 属性返回集合中的元素个数。
  • forEach() 方法用于遍历集合中的所有元素。
  • clear() 方法用于清空集合中的所有元素。

上一篇:js compare

下一篇:js unicode

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 对象数组合并

js 对象转数组

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站