// 创建一个 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
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站