-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13-sets.js
executable file
·72 lines (55 loc) · 2.73 KB
/
13-sets.js
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
/*SETS
Sets are a linear data structure in which values may only OCCUR ones.
Set is available since ES2015 as a type where equality is checked with ===.
*/
/*CREATING A SET
Set can be created using the Set constructor function. Just as maps, an array or iterable object can be passed to the constructor function to initialize values.
NB: When duplicate values are passed, only one is added.
*/
let days = new Set(
[
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Fri", // dup would be ignored
"Sat",
"Sun"
]
);
console.log(days.size); // 7
/* Set Methods:
add() - adds new unique elements to set
size - contains size of set
delete() - deletes element in a set
clear() - deletes all elements in list
has() - returns a boolean indicating presence of absence of element
keys() - returns iterator container values of the set
values() - returns iterator containing values of set
entries() - returns iterator containing value-value pairs
*/
let wholeNum = [1,2,3,4];
let natNum = new Set(wholeNum);
natNum.add(5)
.add(6)
.add(7);
console.log(natNum.has(6));
console.log(natNum.delete(7));
/* ITERATING OVER SETS
Just like Maps, the keys(), values() and entries() methods are available for iterating over values of the set.
Both the keys() and values() method returns an iterator for the values of the set.
The entries() method returns an array with two entries both which ar values.
*/
for (let day of days.keys()) {
console.log(day);
}
for (let day of days.values()) {
console.log(day);
}
for (let day of days.entries()) {
console.log(day);
}
/* USING WEAK SETS */
/* Analogous to weak maps for maps, there is the weak sets alternativefor sets, represented by the type WeakSet. This means that theobjects that are no longer referenced elsewhere are regularlydeleted from the set during garbage collection. Weak sets, like weakmaps, do not provide the keys(), values(), and entries() methods,and they do not have a size property or a clear() method. Inaddition, it’s also true here that no primitive data types can be addedas values of a weak set.
*/