minor fixes
This commit is contained in:
parent
7725acc4df
commit
8db65194dc
1 changed files with 5 additions and 4 deletions
|
@ -77,7 +77,7 @@ Objects lack many methods that exist for arrays, e.g. `map`, `filter` and others
|
||||||
If we'd like to apply them, then we can use `Object.entries` followed by `Object.fromEntries`:
|
If we'd like to apply them, then we can use `Object.entries` followed by `Object.fromEntries`:
|
||||||
|
|
||||||
1. Use `Object.entries(obj)` to get an array of key/value pairs from `obj`.
|
1. Use `Object.entries(obj)` to get an array of key/value pairs from `obj`.
|
||||||
2. Use array methods on that array, e.g. `map`.
|
2. Use array methods on that array, e.g. `map`, to transform key/value pairs.
|
||||||
3. Use `Object.fromEntries(array)` on the resulting array to turn it back into an object.
|
3. Use `Object.fromEntries(array)` on the resulting array to turn it back into an object.
|
||||||
|
|
||||||
For example, we have an object with prices, and would like to double them:
|
For example, we have an object with prices, and would like to double them:
|
||||||
|
@ -91,12 +91,13 @@ let prices = {
|
||||||
|
|
||||||
*!*
|
*!*
|
||||||
let doublePrices = Object.fromEntries(
|
let doublePrices = Object.fromEntries(
|
||||||
// convert to array, map, and then fromEntries gives back the object
|
// convert prices to array, map key/value pairs into another pair
|
||||||
Object.entries(prices).map(([key, value]) => [key, value * 2])
|
// and then fromEntries gives back the object
|
||||||
|
Object.entries(prices).map(entry => [entry[0], entry[1] * 2])
|
||||||
);
|
);
|
||||||
*/!*
|
*/!*
|
||||||
|
|
||||||
alert(doublePrices.meat); // 8
|
alert(doublePrices.meat); // 8
|
||||||
```
|
```
|
||||||
|
|
||||||
It may look difficult at first sight, but becomes easy to understand after you use it once or twice. We can make powerful chains of transforms this way.
|
It may look difficult at first sight, but becomes easy to understand after you use it once or twice. We can make powerful chains of transforms this way.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue