JavaScript之Set方法属性

本文最后更新于 2025年8月20日 晚上

参考:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/WeakSet

集合构造函数

1
Set();

Set()

Set() 构造函数用于创建并返回一个 Set 对象,需要与 new 关键字配合使用。

Array 的元素可以重复且有序,Set 的元素不可以重复且无序,两者元素的值都可以是任意类型。

接收一个参数:

  • iterable(可选):可迭代对象,它的所有元素将不重复地被添加到新 Set 中。
1
2
3
4
5
new Set([1, 2, 3, 4, 5]);
// Set(5) {1, 2, 3, 4, 5}

new Set();
// Set(0) {size: 0}

集合实例属性

1
Set.prototype.size;

size

size 是访问器属性,用于返回 Set 对象中元素的个数。

集合实例方法

1
2
3
4
5
6
7
8
Set.prototype.add();
Set.prototype.has();
Set.prototype.delete();
Set.prototype.clear();
Set.prototype.keys();
Set.prototype.values();
Set.prototype.entries();
Set.prototype.forEach();

add()

add() 方法用于为 Set 对象的末尾添加一个指定的值,返回 Set 对象本身。

has()

has() 方法用于判断 Set 对象中是否存在指定值,返回布尔值。

delete()

delete() 方法用于移除 Set 对象中指定的值,返回布尔值。

clear()

clear() 方法用于移除 Set 对象中的所有值,返回 undefined。

keys()、values()

values() 方法按照元素插入顺序返回一个具有 Set 对象每个元素值的全新 Iterator 对象。

keys() 方法是这个方法的别名(与 Map 对象相似);两者行为一致,都是返回 Set 对象中的元素值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const set1 = new Set();
set1.add(42);
set1.add("forty-two");

for (const i of set1) console.log(i);
// 42
// forty-two

for (const value of set1.values()) console.log(value);
// 42
// forty-two

for (const value of set1.keys()) console.log(value);
// 42
// forty-two

entries()

entries() 方法返回一个新的迭代器对象 ,这个对象的元素是类似 [value, value] 形式的数组。

value 是集合对象中的每个元素,迭代器对象元素的顺序即集合对象中元素插入的顺序。由于集合对象不像 Map 对象那样拥有 key,然而,为了与 Map 对象的 API 形式保持一致,故使得每一个 entry 的 key 和 value 都拥有相同的值,因而最终返回一个 [value, value] 形式的数组。

1
2
3
4
5
6
7
8
9
10
11
const set1 = new Set();
set1.add(42);
set1.add("forty-two");

for (const i of set1.entries()) console.log(i);
// [42, 42]
// ['forty-two', 'forty-two']

for (const [key, value] of set1.entries()) console.log(key, value);
// 42 42
// forty-two forty-two

forEach()

forEach 方法会根据集合中元素的插入顺序,依次执行提供的回调函数,返回值为 undefined。

接收两个参数:

  • callback(必需):回调函数。
    • currentValue(必需),集合当前元素的值
    • currentKey(可选),集合当前元素的索引值,由于集合没有索引,也表示集合当前元素的值
    • set(可选),集合当前本身
  • thisArg(可选):执行回调函数时 this 绑定对象的值。默认值为 undefined。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const set1 = new Set();
set1.add(42);
set1.add("forty-two");

set1.forEach(function (value) {
console.log(value);
});
// 42
// forty-two

set1.forEach(function (key, value) {
console.log(key, value);
});
// 42 42
// forty-two forty-two

set1.forEach(function (key, value, set) {
console.log(key, value, set);
});
// 42 42 {42, 'forty-two'}
// forty-two forty-two {42, 'forty-two'}

弱集合构造函数

1
WeakSet();

WeakSet()

WeakSet() 构造函数用于创建并返回一个 WeakSet 对象,需要与 new 关键字配合使用。

注意,WeakSet 对象的值只能是 Object 类型,原始数据类型是不能作为值的,值的类型没有限制。

接收一个参数:

  • iterable(可选):可迭代对象,它所有迭代值被添加到新 WeakSet 中。

弱集合实例方法

1
2
3
WeakSet.prototype.add();
WeakSet.prototype.has();
WeakSet.prototype.delete();

add()

add() 方法用于为 WeakSet 对象的末尾添加一个指定的值,返回 WeakSet 对象本身。

has()

has() 方法用于判断 WeakSet 对象中是否存在指定值,返回布尔值。

delete()

delete() 方法用于移除 WeakSet 对象中指定的值,返回布尔值。


JavaScript之Set方法属性
https://xuekeven.github.io/2021/09/09/JavaScript之Set方法属性/
作者
Keven
发布于
2021年9月9日
更新于
2025年8月20日
许可协议