-
Notifications
You must be signed in to change notification settings - Fork 3
/
sets&maps.js
127 lines (94 loc) · 2.81 KB
/
sets&maps.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
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
120
121
122
123
124
125
126
127
/**
* Sets are a new object type with ES6 (ES2015) that allow to create collections of unique values.
* The values in a set can be either simple primitives like strings or integers, but more complex object types like object literals or arrays can also be part of a set.
*/
let animals = new Set();
animals.add('🐷');
animals.add('🐼');
animals.add('🐢');
animals.add('🐿');
console.log(animals.size); // 4
animals.add('🐼');
console.log(animals.size); // 4
console.log(animals.has('🐷')); // true
animals.delete('🐷');
console.log(animals.has('🐷')); // false
animals.forEach(animal => {
console.log(`Hey ${animal}!`);
});
// Hey 🐼!
// Hey 🐢!
// Hey 🐿!
animals.clear();
console.log(animals.size); // 0
let partyItems = new Set(['🍕', '🍾', '🎊']);
let items = partyItems.values();
console.log(items.next());
console.log(items.next());
console.log(items.next());
console.log(items.next().done);
// Object {
// done: false,
// value: "🍕"
// }
// Object {
// done: false,
// value: "🍾"
// }
// Object {
// done: false,
// value: "🎊"
// }
// true
/////////////////////////maps/////////////////
/**
* Unlike with objects, map keys can be of any type,
* even objects or functions.
* It’s also easy to get the size of a map,
* while it’s not as straightforward for objects. On top of that,
* with maps we can iterate in the order in which the values were added,
* contrary to objects where there’s no guarantee about the order.
*/
let things = new Map();
const myFunc = () => '🍕';
things.set('🚗', 'Car');
things.set('🏠', 'House');
things.set('✈️', 'Airplane');
things.set(myFunc, '😄 Key is a function!');
things.size; // 4
things.has('🚗'); // true
things.has(myFunc) // true
things.has(() => '🍕'); // false, not the same reference
things.get(myFunc); // '😄 Key is a function!'
things.delete('✈️');
things.has('✈️'); // false
things.clear();
things.size; // 0
// setting key-value pairs is chainable
things.set('🔧', 'Wrench')
.set('🎸', 'Guitar')
.set('🕹', 'Joystick');
const myMap = new Map();
// Even another map can be a key
things.set(myMap, 'Oh gosh!');
things.size; // 4
things.get(myMap); // 'Oh gosh!'
const funArray = [
['🍾', 'Champagne'],
['🍭', 'Lollipop'],
['🎊', 'Confetti'],
];
let funMap = new Map(funArray);
funMap.get('🍾'); // Champagne
let activities = new Map();
activities.set(1, '🏂');
activities.set(2, '🏎');
activities.set(3, '🚣');
activities.set(4, '🤾');
for (let [nb, activity] of activities) {
console.log(`Activity ${nb} is ${activity}`);
}
// Activity 1 is 🏂
// Activity 2 is 🏎
// Activity 3 is 🚣
// Activity 4 is 🤾