Skip to content

设置

一句话解释

设置 是 集合 的 唯一 值 — duplicates 是 automatically 移除.

用大白话说

Set is a collection that only stores unique values. If you add a value that already exists, nothing happens — the set does not grow. Sets maintain insertion order and can hold any type of value.

Sets are useful for deduplication: 新设置([1, 2, 2, 3]) creates a set with values 1, 2, 3. They are also efficient for membership testing — 设置.有(值) is typically faster than 数组.包含(值) for large collections.

Methods include 添加, 有, 删除, 清晰, and iteration via forEach or for...的. The 大小 property gives the number of unique values. To convert a Set to an array, use [...设置] or 数组.从(设置).

示例

javascript
const set = new Set([1, 2, 2, 3, 3, 3]);
console.log(set.size); // 3
set.add(4);
console.log(set.has(3)); // true
console.log([...set]); // [1, 2, 3, 4]

设置 automatically 移除 duplicates and provide efficient membership 测试.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表