Skip to content
New issue

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

ES6:Set: #60

Open
smileyby opened this issue May 25, 2021 · 0 comments
Open

ES6:Set: #60

smileyby opened this issue May 25, 2021 · 0 comments
Labels

Comments

@smileyby
Copy link
Owner

Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。
语法:new Set([iterable])
返回值:一个新的Set对象
对于基本类型值,Set 可以用于去重,但存在兼容性
Set 常用方法:add、delete、has、clear、forEach
Set 属性:size、length

let mySet = new Set();
mySet.add(1);
// Set(1) {1}
mySet.add(5);
// Set(2) {1, 5}
mySet.add("some text");
// Set(3) {1, 5, "some text"}
mySet.has(1);
// true
mySet.has(3);
// false
mySet.has(5);
// true
mySet.has(Math.sqrt(25));
// true
mySet.has("Some Text".toLowerCase());
// true
mySet.size;
// 3
mySet.delete(5);
// true, 从set中移除5
mySet.has(5);
// false, 5已经被移除
mySet.size;
// 2, 我们刚刚移除了一个值

@smileyby smileyby added the JavaScript ☀️ JavaScript label May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant