reduce improvement
This commit is contained in:
parent
7002488bb5
commit
fd4f7375b7
5 changed files with 68 additions and 4 deletions
|
@ -0,0 +1,6 @@
|
||||||
|
function groupById(array) {
|
||||||
|
return array.reduce((obj, value) => {
|
||||||
|
obj[value.id] = value;
|
||||||
|
return obj;
|
||||||
|
}, {})
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
describe("groupById", function() {
|
||||||
|
|
||||||
|
it("creates an object grouped by id", function() {
|
||||||
|
let users = [
|
||||||
|
{id: 'john', name: "John Smith", age: 20},
|
||||||
|
{id: 'ann', name: "Ann Smith", age: 24},
|
||||||
|
{id: 'pete', name: "Pete Peterson", age: 31},
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.deepEqual(groupById(users), {
|
||||||
|
john: {id: 'john', name: "John Smith", age: 20}
|
||||||
|
ann: {id: 'ann', name: "Ann Smith", age: 24},
|
||||||
|
pete: {id: 'pete', name: "Pete Peterson", age: 31},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("works with an empty array", function() {
|
||||||
|
assert.deepEqual(groupById(users), {});
|
||||||
|
});
|
||||||
|
});
|
37
1-js/05-data-types/05-array-methods/12-reduce-object/task.md
Normal file
37
1-js/05-data-types/05-array-methods/12-reduce-object/task.md
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
importance: 4
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# Create keyed object from array
|
||||||
|
|
||||||
|
Let's say we received an array of users in the form `{id:..., name:..., age... }`.
|
||||||
|
|
||||||
|
Create a function `groupById(arr)` that creates an object from it, with `id` as the key, and array items as values.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```js
|
||||||
|
let users = [
|
||||||
|
{id: 'john', name: "John Smith", age: 20},
|
||||||
|
{id: 'ann', name: "Ann Smith", age: 24},
|
||||||
|
{id: 'pete', name: "Pete Peterson", age: 31},
|
||||||
|
];
|
||||||
|
|
||||||
|
let usersById = groupById(users);
|
||||||
|
|
||||||
|
/*
|
||||||
|
// after the call we have:
|
||||||
|
|
||||||
|
usersById = {
|
||||||
|
john: {id: 'john', name: "John Smith", age: 20}
|
||||||
|
ann: {id: 'ann', name: "Ann Smith", age: 24},
|
||||||
|
pete: {id: 'pete', name: "Pete Peterson", age: 31},
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
|
Such function is really handy when working with server data.
|
||||||
|
|
||||||
|
In this task we assume that `id` is unique. There may be no two array items with the same `id`.
|
||||||
|
|
||||||
|
Please use array `.reduce` method in the solution.
|
|
@ -545,7 +545,7 @@ The methods [arr.reduce](mdn:js/Array/reduce) and [arr.reduceRight](mdn:js/Array
|
||||||
The syntax is:
|
The syntax is:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
let value = arr.reduce(function(previousValue, item, index, array) {
|
let value = arr.reduce(function(accumulator, item, index, array) {
|
||||||
// ...
|
// ...
|
||||||
}, [initial]);
|
}, [initial]);
|
||||||
```
|
```
|
||||||
|
@ -554,14 +554,16 @@ The function is applied to all array elements one after another and "carries on"
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
|
|
||||||
- `previousValue` -- is the result of the previous function call, equals `initial` the first time (if `initial` is provided).
|
- `accumulator` -- is the result of the previous function call, equals `initial` the first time (if `initial` is provided).
|
||||||
- `item` -- is the current array item.
|
- `item` -- is the current array item.
|
||||||
- `index` -- is its position.
|
- `index` -- is its position.
|
||||||
- `array` -- is the array.
|
- `array` -- is the array.
|
||||||
|
|
||||||
As function is applied, the result of the previous function call is passed to the next one as the first argument.
|
As function is applied, the result of the previous function call is passed to the next one as the first argument.
|
||||||
|
|
||||||
Sounds complicated, but it's not if you think about the first argument as the "accumulator" that stores the combined result of all previous execution. And at the end it becomes the result of `reduce`.
|
So, the first argument is essentially the accumulator that stores the combined result of all previous executions. And at the end it becomes the result of `reduce`.
|
||||||
|
|
||||||
|
Sounds complicated?
|
||||||
|
|
||||||
The easiest way to grasp that is by example.
|
The easiest way to grasp that is by example.
|
||||||
|
|
||||||
|
@ -626,7 +628,6 @@ let arr = [];
|
||||||
arr.reduce((sum, current) => sum + current);
|
arr.reduce((sum, current) => sum + current);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
So it's advised to always specify the initial value.
|
So it's advised to always specify the initial value.
|
||||||
|
|
||||||
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.
|
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue