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`:
|
||||
|
||||
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.
|
||||
|
||||
For example, we have an object with prices, and would like to double them:
|
||||
|
@ -91,8 +91,9 @@ let prices = {
|
|||
|
||||
*!*
|
||||
let doublePrices = Object.fromEntries(
|
||||
// convert to array, map, and then fromEntries gives back the object
|
||||
Object.entries(prices).map(([key, value]) => [key, value * 2])
|
||||
// convert prices to array, map key/value pairs into another pair
|
||||
// and then fromEntries gives back the object
|
||||
Object.entries(prices).map(entry => [entry[0], entry[1] * 2])
|
||||
);
|
||||
*/!*
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue