-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter17.js
64 lines (52 loc) · 1.92 KB
/
chapter17.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
// Set()
const set = new Set([1,2,3,3,3,3,3,3]);
set; // Set { 1, 2, 3 }
set.add(10);
// set.clear(); // clear everything inside of array
// set.add(100);
// set; // Set { 100 }
// set.delete(3);
set; // Set { 1, 2, 3, 10 }
console.log(set.has(3)) // true
console.log(set.has(5)); // false
console.log(set.keys()); // { [Iterator] }
console.log([...set.keys()]) // [1, 2, 3, 10 ]
console.log([...set.values()]) // [1, 2, 3, 10 ]
set.forEach( value => {
console.log(value); // 1, 2, 3, 10
}
)
for (let value of set) {
value; // 1, 2, 3, 10
}
// const entries = set.entries();
// entries; // { [Iterator] }
const entries = [...set.entries()];
entries; // [ [ 1, 1 ], [ 2, 2 ], [ 3, 3 ], [ 10, 10 ] ]
for (let [value] of entries) {
value; // 1, 2, 3, 10
}
// Map()
const map = new Map([
['my special key', 10],
['my another key 2', {}]
]);
map.set('key3', true)
console.log(map); // Map { 'my speical key' => 10, 'my another key 2' => {}, 'key3' => true }
const entries1 = map.entries();
console.log(entries1); // { [Iterator] }
for ( let [key, value] of entries1) {
console.log(key); // my speical key, my another key 2, key3
console.log(value); // 10, {}, true
}
map.forEach( (value, key) => {
console.log(value); // 10, {}, true
console.log(key); // my speical key, my another key 2, key3
})
console.log(map.has('key3')); // true
console.log(map.has(10)); // false
console.log(map.get('my special key')); // 10
console.log([...map.values()]) // [ 10, {}, true ]
console.log([...map.keys()]) // [ 'my special key', 'my another key 2', 'key3' ]
map.clear();
console.log(map); // Map { }