We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set
Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。
我们可以使用Set构造函数创建Set实例。
const set1 = new Set(); const set2 = new Set(["a","b","c","d","d","e"]);
我们可以使用add方法向Set实例中添加一个新值,因为add方法返回Set对象,所以我们可以以链式的方式再次使用add。如果一个值已经存在于Set对象中,那么它将不再被添加。
add
set2.add("f"); set2.add("g").add("h").add("i").add("j").add("k").add("k"); // 后一个“k”不会被添加到set对象中,因为它已经存在了
我们可以使用has方法检查Set实例中是否存在特定的值。
has
set2.has("a") // true set2.has("z") // true
我们可以使用size属性获得Set实例的长度。
size
set2.size // returns 10
可以使用clear方法删除 Set 中的数据。
clear
set2.clear();
我们可以使用Set对象来删除数组中重复的元素。
const numbers = [1, 2, 3, 4, 5, 6, 6, 7, 8, 8, 5]; const uniqueNums = [...new Set(numbers)]; // [1,2,3,4,5,6,7,8]
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。
我们可以使用
Set
构造函数创建Set
实例。我们可以使用
add
方法向Set
实例中添加一个新值,因为add
方法返回Set
对象,所以我们可以以链式的方式再次使用add
。如果一个值已经存在于Set
对象中,那么它将不再被添加。我们可以使用
has
方法检查Set
实例中是否存在特定的值。我们可以使用
size
属性获得Set
实例的长度。可以使用
clear
方法删除Set
中的数据。我们可以使用
Set
对象来删除数组中重复的元素。The text was updated successfully, but these errors were encountered: