Skip to content

Commit b46487d

Browse files
authored
Merge pull request #275 from ezzep66/the-real-Object.keys
Object.keys, values, entries
2 parents 65d0461 + 4773cac commit b46487d

File tree

6 files changed

+51
-51
lines changed

6 files changed

+51
-51
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe("sumSalaries", function() {
2-
it("returns sum of salaries", function() {
2+
it("devuelve suma de salarios", function() {
33
let salaries = {
44
"John": 100,
55
"Pete": 300,
@@ -9,7 +9,7 @@ describe("sumSalaries", function() {
99
assert.equal( sumSalaries(salaries), 650 );
1010
});
1111

12-
it("returns 0 for the empty object", function() {
12+
it("devuelve 0 para el objeto vacío", function() {
1313
assert.strictEqual( sumSalaries({}), 0);
1414
});
1515
});

1-js/05-data-types/09-keys-values-entries/01-sum-salaries/solution.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ let salaries = {
1717

1818
alert( sumSalaries(salaries) ); // 650
1919
```
20-
Or, optionally, we could also get the sum using `Object.values` and `reduce`:
20+
Otra opción, también podemos obtener la suma utilizando `Object.values` y `reduce`:
2121

2222
```js
23-
// reduce loops over array of salaries,
24-
// adding them up
25-
// and returns the result
23+
// reduce recorre el array de salarios,
24+
// sumándolos
25+
// y devuelve el resultado
2626
function sumSalaries(salaries) {
2727
return Object.values(salaries).reduce((a, b) => a + b, 0) // 650
2828
}

1-js/05-data-types/09-keys-values-entries/01-sum-salaries/task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ importance: 5
22

33
---
44

5-
# Sum the properties
5+
# Suma las propiedades
66

7-
There is a `salaries` object with arbitrary number of salaries.
7+
Hay un objeto `salaries` con un número arbitrario de salarios.
88

9-
Write the function `sumSalaries(salaries)` that returns the sum of all salaries using `Object.values` and the `for..of` loop.
9+
Escriba la función `sumSalaries(salaries)` que devuelva la suma de todos los salarios utilizando `Object.values` y el bucle `for..of`.
1010

11-
If `salaries` is empty, then the result must be `0`.
11+
Si `salaries` está vacío, entonces el resultado debe ser `0`.
1212

13-
For instance:
13+
Por ejemplo:
1414

1515
```js
1616
let salaries = {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
describe("count", function() {
2-
it("counts the number of properties", function() {
2+
it("cuenta el número de propiedades", function() {
33
assert.equal( count({a: 1, b: 2}), 2 );
44
});
55

6-
it("returns 0 for an empty object", function() {
6+
it("devuelve 0 para un objeto vacío", function() {
77
assert.equal( count({}), 0 );
88
});
99

10-
it("ignores symbolic properties", function() {
10+
it("ignora propiedades simbólicas", function() {
1111
assert.equal( count({ [Symbol('id')]: 1 }), 0 );
1212
});
1313
});

1-js/05-data-types/09-keys-values-entries/02-count-properties/task.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 5
22

33
---
44

5-
# Count properties
5+
# Contar propiedades
66

7-
Write a function `count(obj)` that returns the number of properties in the object:
7+
Escriba una función `count(obj)` que devuelva el número de propiedades en el objeto:
88

99
```js
1010
let user = {
@@ -15,7 +15,7 @@ let user = {
1515
alert( count(user) ); // 2
1616
```
1717

18-
Try to make the code as short as possible.
18+
Trate de hacer el código lo más corto posible.
1919

20-
P.S. Ignore symbolic properties, count only "regular" ones.
20+
PD: Ignore propiedades simbólicas, solamente cuente las propiedades "regulares".
2121

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11

22
# Object.keys, values, entries
33

4-
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.
55

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()`.
77

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.
99

10-
They are supported for:
10+
Son compatibles para:
1111

1212
- `Map`
1313
- `Set`
1414
- `Array`
1515

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.
1717

18-
## Object.keys, values, entries
18+
## Object.keys, valores, entradas
1919

20-
For plain objects, the following methods are available:
20+
Para objetos simples, los siguientes métodos están disponibles:
2121

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]`.
2525

26-
Please note the distinctions (compared to map for example):
26+
Tenga en cuenta las distinciones (en comparación con map, por ejemplo):
2727

28-
| | Map | Object |
28+
| | Map | Objeto |
2929
|-------------|------------------|--------------|
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 |
3232

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()`.
3434

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.
3636

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.
3838

39-
For instance:
39+
Por ejemplo:
4040

4141
```js
4242
let user = {
@@ -49,38 +49,38 @@ let user = {
4949
- `Object.values(user) = ["John", 30]`
5050
- `Object.entries(user) = [ ["name","John"], ["age",30] ]`
5151

52-
Here's an example of using `Object.values` to loop over property values:
52+
Aquí hay un ejemplo del uso de `Object.values` para recorrer los valores de propiedad:
5353

5454
```js run
5555
let user = {
5656
name: "John",
5757
age: 30
5858
};
5959

60-
// loop over values
60+
// bucle sobre los valores
6161
for (let value of Object.values(user)) {
62-
alert(value); // John, then 30
62+
alert(value); // John, luego 30
6363
}
6464
```
6565

66-
```warn header="Object.keys/values/entries ignore symbolic properties"
67-
Just like a `for..in` loop, these methods ignore properties that use `Symbol(...)` as keys.
66+
```warn header="Object.keys/values/entries ignoran propiedades simbólicas"
67+
Al igual que un bucle `for..in`, estos métodos ignoran propiedades que utilizan `Symbol(...)` como nombre de propiedades.
6868
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.
7070
```
7171

7272

73-
## Transforming objects
73+
## Transformando objetos
7474

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.
7676

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`:
7878

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.
8282

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:
8484

8585
```js run
8686
let prices = {
@@ -91,12 +91,12 @@ let prices = {
9191

9292
*!*
9393
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
9595
Object.entries(prices).map(([key, value]) => [key, value * 2])
9696
);
9797
*/!*
9898

9999
alert(doublePrices.meat); // 8
100100
```
101101

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

Comments
 (0)