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
+28-28
Original file line number
Diff line number
Diff line change
@@ -15,12 +15,12 @@ But that's not enough for real life. That's why `Map` and `Set` also exist.
15
15
Methods and properties are:
16
16
17
17
-`new Map()` -- creates the map.
18
-
-[`map.set(key, value)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set) -- stores the value by the key.
19
-
-[`map.get(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get) -- returns the value by the key, `undefined` if `key` doesn't exist in map.
20
-
-[`map.has(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has) -- returns `true` if the `key` exists, `false` otherwise.
21
-
-[`map.delete(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete) -- removes the value by the key.
22
-
-[`map.clear()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear) -- removes everything from the map.
23
-
-[`map.size`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size) -- returns the current element count.
18
+
-[`map.set(key, value)`](mdn:js/Map/set) -- stores the value by the key.
19
+
-[`map.get(key)`](mdn:js/Map/get) -- returns the value by the key, `undefined` if `key` doesn't exist in map.
20
+
-[`map.has(key)`](mdn:js/Map/has) -- returns `true` if the `key` exists, `false` otherwise.
21
+
-[`map.delete(key)`](mdn:js/Map/delete) -- removes the value by the key.
22
+
-[`map.clear()`](mdn:js/Map/clear) -- removes everything from the map.
23
+
-[`map.size`](mdn:js/Map/size) -- returns the current element count.
24
24
25
25
For instance:
26
26
@@ -105,9 +105,9 @@ map.set('1', 'str1')
105
105
106
106
For looping over a `map`, there are 3 methods:
107
107
108
-
- [`map.keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys) -- returns an iterable for keys,
109
-
- [`map.values()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values) -- returns an iterable for values,
110
-
- [`map.entries()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries) -- returns an iterable for entries `[key, value]`, it's used by default in `for..of`.
108
+
- [`map.keys()`](mdn:js/Map/keys) -- returns an iterable for keys,
109
+
- [`map.values()`](mdn:js/Map/values) -- returns an iterable for values,
110
+
- [`map.entries()`](mdn:js/Map/entries) -- returns an iterable for entries `[key, value]`, it's used by default in `for..of`.
111
111
112
112
For instance:
113
113
@@ -238,11 +238,11 @@ A `Set` is a special type collection - "set of values" (without keys), where eac
238
238
Its main methods are:
239
239
240
240
- `new Set(iterable)` -- creates the set, and if an `iterable` object is provided (usually an array), copies values from it into the set.
241
-
- `set.add(value)` -- adds a value, returns the set itself.
242
-
- `set.delete(value)` -- removes the value, returns `true` if `value` existed at the moment of the call, otherwise `false`.
243
-
- `set.has(value)` -- returns `true` if the value exists in the set, otherwise `false`.
244
-
- `set.clear()` -- removes everything from the set.
245
-
- `set.size` -- is the elements count.
241
+
- [`set.add(value)`](mdn:js/Set/add) -- adds a value, returns the set itself.
242
+
- [`set.delete(value)`](mdn:js/Set/delete) -- removes the value, returns `true` if `value` existed at the moment of the call, otherwise `false`.
243
+
- [`set.has(value)`](mdn:js/Set/has) -- returns `true` if the value exists in the set, otherwise `false`.
244
+
- [`set.clear()`](mdn:js/Set/clear) -- removes everything from the set.
245
+
- [`set.size`](mdn:js/Set/size) -- is the elements count.
246
246
247
247
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.
248
248
@@ -295,9 +295,9 @@ That's for compatibility with `Map` where the callback passed `forEach` has thre
295
295
296
296
The same methods `Map` has for iterators are also supported:
297
297
298
-
- `set.keys()` -- returns an iterable object for values,
299
-
- `set.values()` -- same as `set.keys()`, for compatibility with `Map`,
300
-
- `set.entries()` -- returns an iterable object for entries `[value, value]`, exists for compatibility with `Map`.
298
+
- [`set.keys()`](mdn:js/Set/keys) -- returns an iterable object for values,
299
+
- [`set.values()`](mdn:js/Set/values) -- same as `set.keys()`, for compatibility with `Map`,
300
+
- [`set.entries()`](mdn:js/Set/entries) -- returns an iterable object for entries `[value, value]`, exists for compatibility with `Map`.
301
301
302
302
## Summary
303
303
@@ -306,12 +306,12 @@ The same methods `Map` has for iterators are also supported:
306
306
Methods and properties:
307
307
308
308
- `new Map([iterable])` -- creates the map, with optional `iterable` (e.g. array) of `[key,value]` pairs for initialization.
309
-
- `map.set(key, value)` -- stores the value by the key, returns the map itself.
310
-
- `map.get(key)` -- returns the value by the key, `undefined` if `key` doesn't exist in map.
311
-
- `map.has(key)` -- returns `true` if the `key` exists, `false` otherwise.
312
-
- `map.delete(key)` -- removes the value by the key, returns `true` if `key` existed at the moment of the call, otherwise `false`.
313
-
- `map.clear()` -- removes everything from the map.
314
-
- `map.size` -- returns the current element count.
309
+
- [`map.set(key, value)`](mdn:js/Map/set) -- stores the value by the key, returns the map itself.
310
+
- [`map.get(key)`](mdn:js/Map/get)` -- returns the value by the key, `undefined` if `key` doesn't exist in map.
311
+
- [`map.has(key)`](mdn:js/Map/has) -- returns `true` if the `key` exists, `false` otherwise.
312
+
- [`map.delete(key)`](mdn:js/Map/delete) -- removes the value by the key, returns `true` if `key` existed at the moment of the call, otherwise `false`.
313
+
- [`map.clear()`](mdn:js/Map/clear) -- removes everything from the map.
314
+
- [`map.size`](mdn:js/Map/size) -- returns the current element count.
315
315
316
316
The differences from a regular `Object`:
317
317
@@ -323,10 +323,10 @@ The differences from a regular `Object`:
323
323
Methods and properties:
324
324
325
325
- `new Set([iterable])` -- creates the set, with optional `iterable` (e.g. array) of values for initialization.
326
-
- `set.add(value)` -- adds a value (does nothing if `value` exists), returns the set itself.
327
-
- `set.delete(value)` -- removes the value, returns `true` if `value` existed at the moment of the call, otherwise `false`.
328
-
- `set.has(value)` -- returns `true` if the value exists in the set, otherwise `false`.
329
-
- `set.clear()` -- removes everything from the set.
330
-
- `set.size` -- is the elements count.
326
+
- [`set.add(value)`](mdn:js/Set/add) -- adds a value (does nothing if `value` exists), returns the set itself.
327
+
- [`set.delete(value)`](mdn:js/Set/delete) -- removes the value, returns `true` if `value` existed at the moment of the call, otherwise `false`.
328
+
- [`set.has(value)`](mdn:js/Set/has) -- returns `true` if the value exists in the set, otherwise `false`.
329
+
- [`set.clear()`](mdn:js/Set/clear) -- removes everything from the set.
330
+
- [`set.size`](mdn:js/Set/size) -- is the elements count.
331
331
332
332
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.
0 commit comments