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
Let's step away from the individual data structures and talk about the iterations over them.
4
+
Alejémonos de las estructuras de datos individuales y hablemos sobre las iteraciones sobre ellas.
5
5
6
-
In the previous chapter we saw methods`map.keys()`, `map.values()`, `map.entries()`.
6
+
En el capítulo anterior vimos métodos`map.keys()`, `map.values()`, `map.entries()`.
7
7
8
-
These methods are generic, there is a common agreement to use them for data structures. If we ever create a data structure of our own, we should implement them too.
8
+
Estos métodos son genéricos, existe un acuerdo común para usarlos para estructuras de datos. Si alguna vez creamos una estructura de datos propia, también deberíamos implementarla.
9
9
10
-
They are supported for:
10
+
Son compatibles para:
11
11
12
12
-`Map`
13
13
-`Set`
14
14
-`Array`
15
15
16
-
Plain objects also support similar methods, but the syntax is a bit different.
16
+
Los objetos simples también admiten métodos similares, pero la sintaxis es un poco diferente.
17
17
18
-
## Object.keys, values, entries
18
+
## Object.keys, valores, entradas
19
19
20
-
For plain objects, the following methods are available:
20
+
Para objetos simples, los siguientes métodos están disponibles:
21
21
22
-
-[Object.keys(obj)](mdn:js/Object/keys) -- returns an array of keys.
23
-
-[Object.values(obj)](mdn:js/Object/values) -- returns an array of values.
24
-
-[Object.entries(obj)](mdn:js/Object/entries) -- returns an array of `[key, value]` pairs.
22
+
-[Object.keys(obj)](mdn:js/Object/keys) -- devuelve un array de propiedades.
23
+
-[Object.values(obj)](mdn:js/Object/values) -- devuelve un array de valores.
24
+
-[Object.entries(obj)](mdn:js/Object/entries) -- devuelve un array de pares `[propiedad, valor]`.
25
25
26
-
Please note the distinctions (compared to map for example):
26
+
Tenga en cuenta las distinciones (en comparación con map, por ejemplo):
27
27
28
-
|| Map |Object|
28
+
|| Map |Objeto|
29
29
|-------------|------------------|--------------|
30
-
|Call syntax |`map.keys()`|`Object.keys(obj)`, but not`obj.keys()`|
31
-
|Returns | iterable | "real" Array |
30
+
|Sintaxis de llamada |`map.keys()`|`Object.keys(obj)`, pero no`obj.keys()`|
31
+
|Devuelve| iterable | "real" Array |
32
32
33
-
The first difference is that we have to call `Object.keys(obj)`, and not`obj.keys()`.
33
+
La primera diferencia es que tenemos que llamar `Object.keys(obj)`, y no`obj.keys()`.
34
34
35
-
Why so? The main reason is flexibility. Remember, objects are a base of all complex structures in JavaScript. So we may have an object of our own like `data`that implements its own `data.values()` method. And we still can call`Object.values(data)`on it.
35
+
¿Por qué? La razón principal es la flexibilidad. Recuerda, los objetos son una base de todas las estructuras complejas en JavaScript. Entonces, podemos tener un objeto propio como `data`que implementa su propio método `data.values()`. Y todavía podemos llamar a`Object.values(data)`en él.
36
36
37
-
The second difference is that `Object.*`methods return "real" array objects, not just an iterable. That's mainly for historical reasons.
37
+
La segunda diferencia es que los métodos `Object.*`devuelven objetos de array "reales", no solo un iterable. Eso es principalmente por razones históricas.
Al igual que un bucle `for..in`, estos métodos ignoran propiedades que utilizan `Symbol(...)` como nombre de propiedades.
68
68
69
-
Usually that's convenient. But if we want symbolic keys too, then there's a separate method [Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols) that returns an array of only symbolic keys. Also, there exist a method [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) that returns *all* keys.
69
+
Normalmente, esto es conveniente. Pero si también queremos propiedades simbólicas, entonces hay un método aparte [Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols) que devuelve un array de únicamente propiedades simbólicas. También existe un método [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) que devuelve *todas* las propiedades.
70
70
```
71
71
72
72
73
-
## Transforming objects
73
+
## Transformando objetos
74
74
75
-
Objects lack many methods that exist for arrays, e.g. `map`,`filter`and others.
75
+
Los objetos carecen de muchos métodos que existen para los arrays, p. Ej. `map`,`filter`y otros.
76
76
77
-
If we'd like to apply them, then we can use `Object.entries`followed by`Object.fromEntries`:
77
+
Si queremos aplicarlos, entonces podemos usar `Object.entries`seguido de`Object.fromEntries`:
78
78
79
-
1. Use `Object.entries(obj)`to get an array of key/value pairs from`obj`.
80
-
2. Use array methods on that array, e.g. `map`.
81
-
3. Use `Object.fromEntries(array)`on the resulting array to turn it back into an object.
79
+
1. Use `Object.entries(obj)`para obtener un array de pares propiedad/valor de`obj`.
80
+
2. Use métodos de array en ese array, p.ej. `map`.
81
+
3. Use `Object.fromEntries(array)`en el array resultante para convertirlo nuevamente en un objeto.
82
82
83
-
For example, we have an object with prices, and would like to double them:
83
+
Por ejemplo, tenemos un objeto con precios y nos gustaría duplicarlos:
84
84
85
85
```js run
86
86
let prices = {
@@ -91,12 +91,12 @@ let prices = {
91
91
92
92
*!*
93
93
let doublePrices =Object.fromEntries(
94
-
//convert to array, map, and then fromEntries gives back the object
94
+
//convertir a array, map, y luego fromEntries nos devuelve el objeto
95
95
Object.entries(prices).map(([key, value]) => [key, value *2])
96
96
);
97
97
*/!*
98
98
99
99
alert(doublePrices.meat); // 8
100
100
```
101
101
102
-
It may look difficult from the first sight, but becomes easy to understand after you use it once or twice. We can make powerful chains of transforms this way.
102
+
Puede parecer difícil a primera vista, pero se vuelve fácil de entender después de usarlo una o dos veces. Podemos hacer poderosas cadenas de transformaciones de esta manera.
0 commit comments