|
1 | 1 |
|
2 | 2 | # Map and Set |
3 | 3 |
|
4 | | -Till now, we've learned about the following complex data structures: |
| 4 | +Până acum, am învățat despre următoarele structuri complexe de date: |
5 | 5 |
|
6 | | -- Objects are used for storing keyed collections. |
7 | | -- Arrays are used for storing ordered collections. |
| 6 | +- Obiectele sunt utilizate pentru stocarea colecțiilor cu chei. |
| 7 | +- Array-urile sunt utilizate pentru stocarea colecțiilor ordonate. |
8 | 8 |
|
9 | | -But that's not enough for real life. That's why `Map` and `Set` also exist. |
| 9 | +Dar acest lucru nu este suficient pentru viața reală. De aceea există și `Map` și `Set`. |
10 | 10 |
|
11 | 11 | ## Map |
12 | 12 |
|
13 | | -[Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) is a collection of keyed data items, just like an `Object`. But the main difference is that `Map` allows keys of any type. |
| 13 | +[Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) este o colecție de elemente de date cu chei, exact ca un `Object`. Dar diferența principală este că `Map` permite chei de orice tip. |
14 | 14 |
|
15 | | -Methods and properties are: |
| 15 | +Metodele și proprietățile sunt: |
16 | 16 |
|
17 | | -- [`new Map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/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 element (the key/value pair) 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. |
| 17 | +- [`new Map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/Map) -- creează map. |
| 18 | +- [`map.set(key, value)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set) -- stochează valoarea în funcție de cheie. |
| 19 | +- [`map.get(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get) -- returnează valoarea în funcție de cheie, `undefined` dacă `key` nu există în map. |
| 20 | +- [`map.has(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has) -- returnează `true` dacă `key` există, `false` în caz contrar. |
| 21 | +- [`map.delete(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete) -- elimină elementul (perechea cheie/valoare) în funcție de cheie. |
| 22 | +- [`map.clear()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear) -- elimină totul din map. |
| 23 | +- [`map.size`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size) -- returnează numărul curent de elemente. |
24 | 24 |
|
25 | | -For instance: |
| 25 | +De exemplu: |
26 | 26 |
|
27 | 27 | ```js run |
28 | 28 | let map = new Map(); |
29 | 29 |
|
30 | | -map.set('1', 'str1'); // a string key |
31 | | -map.set(1, 'num1'); // a numeric key |
32 | | -map.set(true, 'bool1'); // a boolean key |
| 30 | +map.set('1', 'str1'); // o cheie string |
| 31 | +map.set(1, 'num1'); // o cheie numerică |
| 32 | +map.set(true, 'bool1'); // o cheie boolean |
33 | 33 |
|
34 | | -// remember the regular Object? it would convert keys to string |
35 | | -// Map keeps the type, so these two are different: |
| 34 | +// vă amintiți de obiectul obișnuit Object? acesta ar converti cheile în șiruri de caractere |
| 35 | +// Map păstrează tipul, deci acestea două sunt diferite: |
36 | 36 | alert( map.get(1) ); // 'num1' |
37 | 37 | alert( map.get('1') ); // 'str1' |
38 | 38 |
|
39 | 39 | alert( map.size ); // 3 |
40 | 40 | ``` |
41 | 41 |
|
42 | | -As we can see, unlike objects, keys are not converted to strings. Any type of key is possible. |
| 42 | +După cum putem vedea, spre deosebire de obiecte, cheile nu sunt convertite în șiruri de caractere. Orice tip de cheie este posibilă. |
43 | 43 |
|
44 | | -```smart header="`map[key]` isn't the right way to use a `Map`" |
45 | | -Although `map[key]` also works, e.g. we can set `map[key] = 2`, this is treating `map` as a plain JavaScript object, so it implies all corresponding limitations (only string/symbol keys and so on). |
| 44 | +```smart header="`map[key]` nu este modalitatea corectă de utilizare a unui `Map`" |
| 45 | +Deși `map[key]` funcționează de asemenea, e.g. putem seta `map[key] = 2`, acest lucru tratează `map` ca pe un simplu obiect JavaScript, deci implică toate limitările corespunzătoare (numai chei de tip string/simbol și așa mai departe). |
46 | 46 |
|
47 | | -So we should use `map` methods: `set`, `get` and so on. |
| 47 | +Deci ar trebui să folosim metodele `map`: `set`, `get` și așa mai departe. |
48 | 48 | ``` |
49 | 49 |
|
50 | | -**Map can also use objects as keys.** |
| 50 | +**Map poate folosi și obiecte ca și chei.** |
51 | 51 |
|
52 | 52 | For instance: |
53 | 53 |
|
|
0 commit comments