You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/07-map-set/article.md
+27-9Lines changed: 27 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ But that's not enough for real life. That's why `Map` and `Set` also exist.
12
12
13
13
[Map](mdn:js/Map) is a collection of keyed data items, just like an `Object`. But the main difference is that `Map` allows keys of any type.
14
14
15
-
The main methods are:
15
+
Methods and properties are:
16
16
17
17
-`new Map()` -- creates the map.
18
18
-`map.set(key, value)` -- stores the value by the key.
@@ -57,7 +57,7 @@ visitsCountMap.set(john, 123);
57
57
alert( visitsCountMap.get(john) ); // 123
58
58
```
59
59
60
-
Using objects as keys is one of most notable and important `Map` features. For string keys, `Object` can be fine, but it would be difficult to replace the `Map` with a regular `Object` in the example above.
60
+
Using objects as keys is one of most notable and important `Map` features. For string keys, `Object` can be fine, but not for object keys.
61
61
62
62
Let's try:
63
63
@@ -94,7 +94,7 @@ map.set('1', 'str1')
94
94
95
95
## Map from Object
96
96
97
-
When a `Map` is created, we can pass an array (or another iterable) with key-value pairs, like this:
97
+
When a `Map` is created, we can pass an array (or another iterable) with key-value pairs for initialization, like this:
98
98
99
99
```js
100
100
// array of [key, value] pairs
@@ -105,7 +105,7 @@ let map = new Map([
105
105
]);
106
106
```
107
107
108
-
There is a built-in method [Object.entries(obj)](mdn:js/Object/entries) that returns an array of key/value pairs for an object exactly in that format.
108
+
If we have a plain object, and we'd like to create a `Map` from it, then we can use built-in method [Object.entries(obj)](mdn:js/Object/entries) that returns an array of key/value pairs for an object exactly in that format.
109
109
110
110
So we can initialize a map from an object like this:
A `Set` is a collection of values (without keys), where each value may occur only once.
173
+
A `Set` is a special type collection - "set of values" (without keys), where each value may occur only once.
175
174
176
175
Its main methods are:
177
176
@@ -182,6 +181,8 @@ Its main methods are:
182
181
- `set.clear()` -- removes everything from the set.
183
182
- `set.size` -- is the elements count.
184
183
184
+
The main feature is that repeated calls of `set.add(value)` with the same value don't do anything. That's the reason why each value appears in a `Set` only once.
185
+
185
186
For example, we have visitors coming, and we'd like to remember everyone. But repeated visits should not lead to duplicates. A visitor must be "counted" only once.
186
187
187
188
`Set` is just the right thing for that:
@@ -239,13 +240,30 @@ The same methods `Map` has for iterators are also supported:
239
240
240
241
`Map` -- is a collection of keyed values.
241
242
243
+
Methods and properties:
244
+
245
+
- `new Map([iterable])` -- creates the map, with optional `iterable` (e.g. array) of `[key,value]` pairs for initialization.
246
+
- `map.set(key, value)` -- stores the value by the key.
247
+
- `map.get(key)` -- returns the value by the key, `undefined` if `key` doesn't exist in map.
248
+
- `map.has(key)` -- returns `true` if the `key` exists, `false` otherwise.
249
+
- `map.delete(key)` -- removes the value by the key.
250
+
- `map.clear()` -- removes everything from the map.
251
+
- `map.size` -- returns the current element count.
252
+
242
253
The differences from a regular `Object`:
243
254
244
255
- Any keys, objects can be keys.
245
-
- Iterates in the insertion order.
246
256
- Additional convenient methods, the `size` property.
247
257
248
258
`Set` -- is a collection of unique values.
249
259
250
-
- Keeps the insertion order.
251
-
- Doesn't allow to reorder elements.
260
+
Methods and properties:
261
+
262
+
- `new Set([iterable])` -- creates the set, with optional `iterable` (e.g. array) of values for initialization.
263
+
- `set.add(value)` -- adds a value (does nothing if `value` exists), returns the set itself.
264
+
- `set.delete(value)` -- removes the value, returns `true` if `value` existed at the moment of the call, otherwise `false`.
265
+
- `set.has(value)` -- returns `true` if the value exists in the set, otherwise `false`.
266
+
- `set.clear()` -- removes everything from the set.
267
+
- `set.size` -- is the elements count.
268
+
269
+
Iteration over `Map` and `Set` is always in the insertion order, so we can't say that these collections are unordered, but we can't reorder elements or directly get an element by its number.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/08-weakmap-weakset/01-recipients-read/task.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -20,4 +20,4 @@ Now, which data structure you could use to store information whether the message
20
20
21
21
P.S. When a message is removed from `messages`, it should disappear from your structure as well.
22
22
23
-
P.P.S. We shouldn't modify message objects directly. As they are managed by someone else's code, adding extra properties to them may have bad consequences.
23
+
P.P.S. We shouldn't modify message objects, add our properties to them. As they are managed by someone else's code, that may lead to bad consequences.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/08-weakmap-weakset/02-recipients-when-read/task.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -17,3 +17,5 @@ let messages = [
17
17
The question now is: which data structure you'd suggest to store the information: "when the message was read?".
18
18
19
19
In the previous task we only needed to store the "yes/no" fact. Now we need to store the date, and it should only remain in memory until the message is garbage collected.
20
+
21
+
P.S. Dates can be stored as objects of built-in `Date` class, that we'll cover later.
0 commit comments