Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
Y
yifu-study-front-share
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
yifu-study
front
yifu-study-front-share
Commits
b73c3b91
Commit
b73c3b91
authored
Sep 08, 2022
by
anrun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
前端分享
parent
bb8b5d15
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
142 additions
and
0 deletions
+142
-0
浅聊Set.md
source/_posts/浅聊Set.md
+142
-0
No files found.
source/_posts/浅聊Set.md
0 → 100644
View file @
b73c3b91
```
title: 浅聊Set
date: 2022/9/7 18:00:00
author: 张润
```
ES6 新增的一种新的数据结构,类似于数组,但成员是唯一且无序的,没有重复的值。
Set 本身是一种构造函数,用来生成 Set 数据结构。
```
javascript
new
Set
([
iterable
])
```
举个例子:
```
javascript
const
s
=
new
Set
()
[
1
,
2
,
3
,
4
,
3
,
2
,
1
].
forEach
(
x
=>
s
.
add
(
x
))
for
(
let
i
of
s
)
{
console
.
log
(
i
)
// 1 2 3 4
}
// 去重数组的重复对象
let
arr
=
[
1
,
2
,
3
,
2
,
1
,
1
]
[...
new
Set
(
arr
)]
// [1, 2, 3]复制代码
```
Set 对象允许你储存任何类型的唯一值,无论是原始值或者是对象引用。
向 Set 加入值的时候,不会发生类型转换,所以5和"5"是两个不同的值。Set 内部判断两个值是否不同,使用的算法叫做“Same-value-zero equality”,它类似于精确相等运算符(===),主要的区别是NaN等于自身,而精确相等运算符认为NaN不等于自身。
```
javascript
let
set
=
new
Set
();
let
a
=
NaN
;
let
b
=
NaN
;
set
.
add
(
a
);
set
.
add
(
b
);
set
// Set {NaN}
let
set1
=
new
Set
()
set1
.
add
(
5
)
set1
.
add
(
'5'
)
console
.
log
([...
set1
])
// [5, "5"]复制代码
```
Set 实例属性
```
javascript
constructor
:
构造函数
size
:元素数量
let
set
=
new
Set
([
1
,
2
,
3
,
2
,
1
])
console
.
log
(
set
.
length
)
// undefined
console
.
log
(
set
.
size
)
// 3复制代码
```
Set 实例方法-操作方法
```
javascript
add
(
value
)
:新增,相当于
array
里的
push
delete
(
value
)
:存在即删除集合中
value
复制代码
has
(
value
)
:判断集合中是否存在
value
复制代码
clear
()
:清空集合复制代码
```
```
javascript
let
set
=
new
Set
()
set
.
add
(
1
).
add
(
2
).
add
(
1
)
set
.
has
(
1
)
// true
set
.
has
(
3
)
// false
set
.
delete
(
1
)
set
.
has
(
1
)
// false复制代码
```
Array.from 方法可以将 Set 结构转为数组
```
javascript
const
items
=
new
Set
([
1
,
2
,
3
,
2
])
const
array
=
Array
.
from
(
items
)
console
.
log
(
array
)
// [1, 2, 3]
// 或
const
arr
=
[...
items
]
console
.
log
(
arr
)
// [1, 2, 3]复制代码
```
遍历方法(遍历顺序为插入顺序)
1、keys():返回一个包含集合中所有键的迭代器复制代
2、values():返回一个包含集合中所有值得迭代器复制代码
3、ntries():返回一个包含Set对象中所有元素得键值对迭代器复制代码
4、forEach(callbackFn, thisArg):用于对集合成员执行callbackFn操作,如果
提供了 thisArg 参数,回调中的this会是这个参数,没有返回值复制代
```
javascript
let
set
=
new
Set
([
1
,
2
,
3
])
console
.
log
(
set
.
keys
())
// SetIterator {1, 2, 3}
console
.
log
(
set
.
values
())
// SetIterator {1, 2, 3}
console
.
log
(
set
.
entries
())
// SetIterator {1, 2, 3}
for
(
let
item
of
set
.
keys
())
{
console
.
log
(
item
);
}
// 1 2 3
for
(
let
item
of
set
.
entries
())
{
console
.
log
(
item
);
}
// [1, 1] [2, 2] [3, 3]
set
.
forEach
((
value
,
key
)
=>
{
console
.
log
(
key
+
' : '
+
value
)
})
// 1 : 1 2 : 2 3 : 3
console
.
log
([...
set
])
// [1, 2, 3]复制代码
```
Set 可默认遍历,默认迭代器生成函数是 values() 方法
```
javascript
Set
.
prototype
[
Symbol
.
iterator
]
===
Set
.
prototype
.
values
// true
```
所以, Set可以使用 map、filter 方法
```
javascript
let
set
=
new
Set
([
1
,
2
,
3
])
set
=
new
Set
([...
set
].
map
(
item
=>
item
*
2
))
console
.
log
([...
set
])
// [2, 4, 6]
set
=
new
Set
([...
set
].
filter
(
item
=>
(
item
>=
4
)))
console
.
log
([...
set
])
//[4, 6]
```
因此,Set 很容易实现交集(Intersect)、并集(Union)、差集(Difference)
```
javascript
let
set1
=
new
Set
([
1
,
2
,
3
])
let
set2
=
new
Set
([
4
,
3
,
2
])
let
intersect
=
new
Set
([...
set1
].
filter
(
value
=>
set2
.
has
(
value
)))
let
union
=
new
Set
([...
set1
,
...
set2
])
let
difference
=
new
Set
([...
set1
].
filter
(
value
=>
!
set2
.
has
(
value
)))
console
.
log
(
intersect
)
// Set {2, 3}
console
.
log
(
union
)
// Set {1, 2, 3, 4}
console
.
log
(
difference
)
// Set {1}
```
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment