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
+35-35Lines changed: 35 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,88 +10,88 @@ Mas isso não é suficiente para a vida real. É por isso que `Map` e `Set` tamb
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)é uma coleção de itens de dados indexados, assim como um`Object`. Mas a principal diferença é que`Map`permite chaves de qualquer tipo.
14
14
15
-
Methods and properties are:
15
+
Métodos e propriedades são:
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) -- cria o mapa.
18
+
-[`map.set(chave, valor)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set) -- armazena o valor pela chave.
19
+
-[`map.get(chave)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get) -- retorna o valor pela chave, `undefined`se a `chave` não existir no mapa.
20
+
-[`map.has(chave)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has) -- retorna`true`se a `chave` existir, `false`caso contrário.
21
+
-[`map.delete(chave)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete) -- remove o elemento (o par chave/valor) pela chave.
22
+
-[`map.clear()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear) -- remove tudo do mapa.
23
+
-[`map.size`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size) -- retorna a contagem atual de elementos.
24
24
25
-
For instance:
25
+
Por exemplo:
26
26
27
27
```js run
28
28
let map =newMap();
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'); //uma chave de string
31
+
map.set(1, 'num1'); //uma chave numérica
32
+
map.set(true, 'bool1'); //uma chave booleana
33
33
34
-
//remember the regular Object? it would convert keys to string
35
-
// Map keeps the type, so these two are different:
34
+
//lembra do objeto normal? Ele converteria as chaves para string
35
+
// Map mantém o tipo, então esses dois são diferentes:
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
+
Como podemos ver, ao contrário de objetos, as chaves não são convertidas em strings. Qualquer tipo de chave é possível.
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[chave]`não é a maneira correta de usar um`Map`"
45
+
Embora`map[chave]`também funcione, por exemplo, podemos definir`map[chave] = 2`, isso trata o`map`como um objeto JavaScript simples, o que implica todas as limitações correspondentes (apenas chaves de string/símbolo, entre outras).
46
46
47
-
So we should use `map` methods: `set`, `get`and so on.
47
+
Portanto, devemos usar os métodos do `map`: `set`, `get`e assim por diante.
48
48
```
49
49
50
-
**Map can also use objects as keys.**
50
+
**Map também pode usar objetos como chaves.**
51
51
52
-
For instance:
52
+
Por exemplo:
53
53
54
54
```js run
55
55
let john = { name: "John" };
56
56
57
-
// for every user, let's store their visits count
57
+
// para cada usuário, vamos armazenar a contagem de suas visitas.
58
58
let visitsCountMap = new Map();
59
59
60
-
// john is the key for the map
60
+
// john é a chave para o mapa
61
61
visitsCountMap.set(john, 123);
62
62
63
63
alert( visitsCountMap.get(john) ); // 123
64
64
```
65
65
66
-
Using objects as keys is one of the most notable and important `Map` features. The same does not count for`Object`. String as a key in `Object`is fine, but we can't use another`Object`as a key in`Object`.
66
+
Usar objetos como chaves é uma das características mais notáveis e importantes do `Map`. O mesmo não se aplica ao`Object`. Usar uma string como chave em um `Object`é aceitável, mas não podemos usar outro`Object`como chave em um`Object`.
67
67
68
-
Let's try:
68
+
Vamos tentar:
69
69
70
70
```js run
71
71
let john = { name:"John" };
72
72
let ben = { name:"Ben" };
73
73
74
-
let visitsCountObj = {}; //try to use an object
74
+
let visitsCountObj = {}; //tente usar um objeto
75
75
76
-
visitsCountObj[ben] =234; //try to use ben object as the key
77
-
visitsCountObj[john] =123; //try to use john object as the key, ben object will get replaced
76
+
visitsCountObj[ben] =234; //tente usar o objeto ben como chave
77
+
visitsCountObj[john] =123; //tente usar o objeto john como chave, o objeto ben será substituído
As`visitsCountObj`is an object, it converts all `Object` keys, such as `john`and`ben`above, to same string `"[object Object]"`. Definitely not what we want.
85
+
"Como`visitsCountObj`é um objeto, ele converte todas as chaves de `Object`, como `john`e`ben`acima, para a mesma string `"[object Object]"`. Definitivamente, não é o que queremos."
86
86
87
-
```smart header="How `Map`compares keys"
88
-
To test keys for equivalence, `Map`uses the algorithm[SameValueZero](https://tc39.github.io/ecma262/#sec-samevaluezero). It is roughly the same as strict equality `===`, but the difference is that`NaN`is considered equal to`NaN`. So`NaN`can be used as the key as well.
87
+
```smart header="Como o `Map`compara chaves"
88
+
Para testar as chaves quanto à equivalência, o `Map`utiliza o algoritmo[SameValueZero](https://tc39.github.io/ecma262/#sec-samevaluezero). Ele é essencialmente o mesmo que a igualdade estrita `===`, mas a diferença é que`NaN`é considerado igual a`NaN`. Portanto,`NaN`pode ser usado como chave também.
89
89
90
-
This algorithm can't be changed or customized.
90
+
Este algoritmo não pode ser alterado ou personalizado.
91
91
```
92
92
93
-
````smart header="Chaining"
94
-
Every `map.set` call returns the map itself, so we can "chain" the calls:
93
+
````smart header="Encadeamento"
94
+
Cada chamada de `map.set` retorna o próprio mapa, então podemos "encadear" as chamadas:
0 commit comments