-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset集合.html
119 lines (99 loc) · 3.85 KB
/
set集合.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!--
* @Author: Gatsby
* @Date: 2021-03-26 13:47:30
* @LastEditTime: 2021-03-26 15:09:55
* @LastEditors: Gatsby
* @Description:
* @FilePath: \ES6\set集合.html
* 可以输入预定的版权声明、个性签名、空行等
-->
<!--
* @Author: Gatsby
* @Date: 2021-03-26 13:47:30
* @LastEditTime: 2021-03-26 15:06:44
* @LastEditors: Gatsby
* @Description:
* @FilePath: \ES6\set集合.html
* 可以输入预定的版权声明、个性签名、空行等
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//ES6 提供的新的数据结构 Set (集合), 类似于数组, 但是成员的值是唯一的,集合实现了 iterator 接口,所以可以使用 "扩展运算符" 和 "for...of" 进行遍历
//集合的属性和方法: size add delete has
/*size 返回集合的元素个数
add 增加一个新元素,返回当前集合
delete 删除元素,返回 Boolean 值
has 检测集合中是否包含某个元素,返回 Boolean 值
*/
//声明一个集合
let set_1 = new Set();
console.log(set_1, typeof set_1); //Set(0) {} "object"
let set_2 = new Set(['a', 'b', 'c', 'd', 'e']);
console.log(set_2, typeof set_2); //Set(5) {"a", "b", "c", "d", "e"} "object"
//输出元素个数
console.log(set_2.size); //5
//添加新元素 向 Set 加入值的时候,不会发生类型转换,所以5和"5"是两个不同的值。
set_2.add('aa');
console.log(set_2); //Set(6) {"a", "b", "c", "d", "e", 'aa'}
//删除元素
set_2.delete('a');
console.log(set_2); //Set(5) {"b", "c", "d", "e", "aa"}
//判断集合中是否含有某元素
console.log(set_2.has('aa')); //true
console.log(set_2.has('a')); //false
//清空集合
// set_2.clear();
// console.log(set_2); //Set(0) {}
//遍历集合
for (let val of set_2) {
console.log(val);
/*
set集合.html: 58 c
set集合.html: 58 d
set集合.html: 58 e
set集合.html: 58 aa
*/
}
// Set 集合实践
let arr_1 = [1, 2, 2, 23, 3, 3, 34, 5, 5, 6, 75, 6, 9, 9, 10]
console.log(arr_1.length); //15
//1.数组去重
// let res_1 = new Set(arr_1);
// console.log(res_1); //set(10) {1, 2, 23, 3, 34, …}
let res_1 = [...new Set(arr_1)];
console.log(res_1); //[1, 2, 23, 3, 34, 5, 6, 75, 9, 10]
//2. 输出数组交集
//方法一:
let arr_2 = [1, 3, 33, 4, 5, 6, 7, 88, 4, 9, 5, 3, 6, 10, 9];
console.log(arr_2.length); //15
let result = [...new Set(arr_1)].filter(item => {
let res_2 = new Set(arr_2);
if (res_2.has(item)) {
return true;
} else {
return false;
}
});
console.log(result); //(6) [1, 3, 5, 6, 9, 10]
//方法二:
let result_2 = [...new Set(arr_1)].filter(item => new Set(arr_2).has(item));
console.log(result_2); //(6) [1, 3, 5, 6, 9, 10]
//3.输出数组并集
// let arr_3 = [1, 3, 33, 4, 5, 6, 7, 88, 4, 9, 5, 3, 6, 10, 9];
let union = [...new Set([...arr_1, ...arr_2])];
console.log(union); //(14) [1, 2, 23, 3, 34, 5, 6, 75, 9, 10, 33, 4, 7, 88]
//4.输出差集
// arr_1 中包含, 但是 arr_2 中没有
let diff = [...new Set(arr_1)].filter(item => !(new Set(arr_2).has(item)));
console.log(diff); //(4) [2, 23, 34, 75]
</script>
</body>
</html>