Skip to content

Commit 76b6eaf

Browse files
committed
fixes
1 parent b11005c commit 76b6eaf

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

1-js/05-data-types/07-map-set/article.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ But that's not enough for real life. That's why `Map` and `Set` also exist.
1212

1313
[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.
1414

15-
The main methods are:
15+
Methods and properties are:
1616

1717
- `new Map()` -- creates the map.
1818
- `map.set(key, value)` -- stores the value by the key.
@@ -57,7 +57,7 @@ visitsCountMap.set(john, 123);
5757
alert( visitsCountMap.get(john) ); // 123
5858
```
5959

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.
6161

6262
Let's try:
6363

@@ -94,7 +94,7 @@ map.set('1', 'str1')
9494
9595
## Map from Object
9696
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:
9898
9999
```js
100100
// array of [key, value] pairs
@@ -105,7 +105,7 @@ let map = new Map([
105105
]);
106106
```
107107
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.
109109
110110
So we can initialize a map from an object like this:
111111
@@ -168,10 +168,9 @@ recipeMap.forEach( (value, key, map) => {
168168
});
169169
```
170170
171-
172171
## Set
173172
174-
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.
175174
176175
Its main methods are:
177176
@@ -182,6 +181,8 @@ Its main methods are:
182181
- `set.clear()` -- removes everything from the set.
183182
- `set.size` -- is the elements count.
184183
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+
185186
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.
186187
187188
`Set` is just the right thing for that:
@@ -239,13 +240,30 @@ The same methods `Map` has for iterators are also supported:
239240
240241
`Map` -- is a collection of keyed values.
241242
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+
242253
The differences from a regular `Object`:
243254
244255
- Any keys, objects can be keys.
245-
- Iterates in the insertion order.
246256
- Additional convenient methods, the `size` property.
247257
248258
`Set` -- is a collection of unique values.
249259
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.

1-js/05-data-types/08-weakmap-weakset/01-recipients-read/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ messages[0][isRead] = true;
4040

4141
Now third-party code probably won't see our extra property.
4242

43-
Both solutions are possible, though the one with `WeakSet` is "cleaner" from the architectural point of view.
43+
Although symbols allow to lower the probability of problems, using `WeakSet` is better from the architectural point of view.

1-js/05-data-types/08-weakmap-weakset/01-recipients-read/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ Now, which data structure you could use to store information whether the message
2020

2121
P.S. When a message is removed from `messages`, it should disappear from your structure as well.
2222

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.

1-js/05-data-types/08-weakmap-weakset/02-recipients-when-read/task.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ let messages = [
1717
The question now is: which data structure you'd suggest to store the information: "when the message was read?".
1818

1919
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

Comments
 (0)