Skip to content

Commit fd4f737

Browse files
committed
reduce improvement
1 parent 7002488 commit fd4f737

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function groupById(array) {
2+
return array.reduce((obj, value) => {
3+
obj[value.id] = value;
4+
return obj;
5+
}, {})
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
describe("groupById", function() {
2+
3+
it("creates an object grouped by id", function() {
4+
let users = [
5+
{id: 'john', name: "John Smith", age: 20},
6+
{id: 'ann', name: "Ann Smith", age: 24},
7+
{id: 'pete', name: "Pete Peterson", age: 31},
8+
];
9+
10+
assert.deepEqual(groupById(users), {
11+
john: {id: 'john', name: "John Smith", age: 20}
12+
ann: {id: 'ann', name: "Ann Smith", age: 24},
13+
pete: {id: 'pete', name: "Pete Peterson", age: 31},
14+
});
15+
});
16+
17+
it("works with an empty array", function() {
18+
assert.deepEqual(groupById(users), {});
19+
});
20+
});

1-js/05-data-types/05-array-methods/12-reduce-object/solution.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
importance: 4
2+
3+
---
4+
5+
# Create keyed object from array
6+
7+
Let's say we received an array of users in the form `{id:..., name:..., age... }`.
8+
9+
Create a function `groupById(arr)` that creates an object from it, with `id` as the key, and array items as values.
10+
11+
For example:
12+
13+
```js
14+
let users = [
15+
{id: 'john', name: "John Smith", age: 20},
16+
{id: 'ann', name: "Ann Smith", age: 24},
17+
{id: 'pete', name: "Pete Peterson", age: 31},
18+
];
19+
20+
let usersById = groupById(users);
21+
22+
/*
23+
// after the call we have:
24+
25+
usersById = {
26+
john: {id: 'john', name: "John Smith", age: 20}
27+
ann: {id: 'ann', name: "Ann Smith", age: 24},
28+
pete: {id: 'pete', name: "Pete Peterson", age: 31},
29+
}
30+
*/
31+
```
32+
33+
Such function is really handy when working with server data.
34+
35+
In this task we assume that `id` is unique. There may be no two array items with the same `id`.
36+
37+
Please use array `.reduce` method in the solution.

1-js/05-data-types/05-array-methods/article.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ The methods [arr.reduce](mdn:js/Array/reduce) and [arr.reduceRight](mdn:js/Array
545545
The syntax is:
546546

547547
```js
548-
let value = arr.reduce(function(previousValue, item, index, array) {
548+
let value = arr.reduce(function(accumulator, item, index, array) {
549549
// ...
550550
}, [initial]);
551551
```
@@ -554,14 +554,16 @@ The function is applied to all array elements one after another and "carries on"
554554

555555
Arguments:
556556

557-
- `previousValue` -- is the result of the previous function call, equals `initial` the first time (if `initial` is provided).
557+
- `accumulator` -- is the result of the previous function call, equals `initial` the first time (if `initial` is provided).
558558
- `item` -- is the current array item.
559559
- `index` -- is its position.
560560
- `array` -- is the array.
561561

562562
As function is applied, the result of the previous function call is passed to the next one as the first argument.
563563

564-
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`.
564+
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`.
565+
566+
Sounds complicated?
565567

566568
The easiest way to grasp that is by example.
567569

@@ -626,7 +628,6 @@ let arr = [];
626628
arr.reduce((sum, current) => sum + current);
627629
```
628630

629-
630631
So it's advised to always specify the initial value.
631632

632633
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.

0 commit comments

Comments
 (0)