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
+17-17Lines changed: 17 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ map.set('1', 'str1'); // o cheie string
31
31
map.set(1, 'num1'); // o cheie numerică
32
32
map.set(true, 'bool1'); // o cheie boolean
33
33
34
-
// vă amintiți de obiectul obișnuit Object? acesta ar converti cheile în șiruri de caractere
34
+
// vă amintiți de Obiectul obișnuit? acesta ar converti cheile în șiruri de caractere
35
35
// Map păstrează tipul, deci acestea două sunt diferite:
36
36
alert( map.get(1) ); // 'num1'
37
37
alert( map.get('1') ); // 'str1'
@@ -42,7 +42,7 @@ alert( map.size ); // 3
42
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
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).
45
+
Deși `map[key]` funcționează de asemenea, e.g. putem seta `map[key] = 2`, acest lucru tratează `map` ca pe un obiect obișnuit JavaScript, deci implică toate limitările corespunzătoare (numai chei de tip string/simbol și așa mai departe).
46
46
47
47
Deci ar trebui să folosim metodele `map`: `set`, `get` și așa mai departe.
48
48
```
@@ -161,9 +161,9 @@ let map = new Map([
161
161
alert( map.get('1') ); // str1
162
162
```
163
163
164
-
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)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) that returns an array of key/value pairs for an object exactly in that format.
164
+
Dacă avem un obiect obișnuit, și dorim să creăm un `Map` din el, atunci putem folosi metoda încorporată [Object.entries(obj)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) care returnează o matrice de perechi cheie/valoare pentru un obiect exact în acest format.
165
165
166
-
So we can create a map from an object like this:
166
+
Astfel putem crea un map dintr-un obiect în felul următor:
167
167
168
168
```js run
169
169
let obj = {
@@ -178,14 +178,14 @@ let map = new Map(Object.entries(obj));
178
178
alert( map.get('name') ); // John
179
179
```
180
180
181
-
Here, `Object.entries` returns the array of key/value pairs: `[ ["name","John"], ["age", 30] ]`. That's what `Map` needs.
181
+
Aici, `Object.entries` returnează matricea de perechi cheie/valoare: `[ ["name","John"], ["age", 30] ]`. De asta are nevoie `Map`.
182
182
183
183
184
-
## Object.fromEntries: Object from Map
184
+
## Object.fromEntries: Obiect din Map
185
185
186
-
We've just seen how to create `Map` from a plain object with `Object.entries(obj)`.
186
+
Tocmai am văzut cum să creăm `Map` dintr-un obiect obișnuit cu `Object.entries(obj)`.
187
187
188
-
There's `Object.fromEntries` method that does the reverse: given an array of `[key, value]` pairs, it creates an object from them:
188
+
Există metoda `Object.fromEntries` care face invers: primind o matrice de perechi `[key, value]`, aceasta creează un obiect din ele:
189
189
190
190
```js run
191
191
let prices = Object.fromEntries([
@@ -194,16 +194,16 @@ let prices = Object.fromEntries([
We can use `Object.fromEntries` to get a plain object from `Map`.
202
+
Putem folosi `Object.fromEntries` pentru a obține un obiect obișnuit din `Map`.
203
203
204
-
E.g. we store the data in a `Map`, but we need to pass it to a 3rd-party code that expects a plain object.
204
+
E.g. stocăm datele într-un `Map`, dar trebuie să le transmitem unui cod terț care așteaptă un obiect obișnuit.
205
205
206
-
Here we go:
206
+
Iată cum procedăm:
207
207
208
208
```js run
209
209
let map = new Map();
@@ -212,23 +212,23 @@ map.set('orange', 2);
212
212
map.set('meat', 4);
213
213
214
214
*!*
215
-
let obj = Object.fromEntries(map.entries()); // make a plain object (*)
215
+
let obj = Object.fromEntries(map.entries()); // face un obiect obișnuit (*)
216
216
*/!*
217
217
218
-
// done!
218
+
// gata!
219
219
// obj = { banana: 1, orange: 2, meat: 4 }
220
220
221
221
alert(obj.orange); // 2
222
222
```
223
223
224
-
A call to `map.entries()` returns an iterable of key/value pairs, exactly in the right format for `Object.fromEntries`.
224
+
Un apel către `map.entries()` returnează un iterabil de perechi cheie/valoare, exact în formatul corect pentru `Object.fromEntries`.
225
225
226
-
We could also make line `(*)` shorter:
226
+
Am putea de asemenea să facem linia `(*)` mai scurtă:
227
227
```js
228
228
let obj = Object.fromEntries(map); // omit .entries()
229
229
```
230
230
231
-
That's the same, because `Object.fromEntries` expects an iterable object as the argument. Not necessarily an array. And the standard iteration for `map` returns same key/value pairs as `map.entries()`. So we get a plain object with same key/values as the `map`.
231
+
Este același lucru, deoarece `Object.fromEntries` așteaptă ca argument un obiect iterabil. Nu neapărat o matrice. Iar iterația standard pentru `map` returnează aceleași perechi cheie/valoare ca și `map.entries()`. Deci obținem un obiect obișnuit cu aceleași chei/valori ca și `map`.
0 commit comments