Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,170 --> 00:00:01,800
All right.
2
00:00:01,800 --> 00:00:05,360
So we tackled the map and the filter method.
3
00:00:05,370 --> 00:00:12,450
And so now it's time for the one method that rules them all, which is the reduce method.
4
00:00:13,580 --> 00:00:21,720
So the reduce method is really the most versatile and the most powerful of all array methods in JavaScript.
5
00:00:21,740 --> 00:00:29,030
So in fact, we could probably implement all the other methods simply using the reduce method, but
6
00:00:29,030 --> 00:00:31,180
that would be a little bit too much work.
7
00:00:31,190 --> 00:00:36,740
And so that's why we have all of these other methods which are a lot easier to use.
8
00:00:36,830 --> 00:00:44,840
So I will really not go deep into the reduce method here because we could spend hours just with this
9
00:00:44,840 --> 00:00:45,590
method.
10
00:00:45,590 --> 00:00:53,600
But I will just show you one of the most common use cases, which is to add together numbers or basically
11
00:00:53,600 --> 00:00:58,130
to perform any operation or mathematical operations with numbers.
12
00:00:58,190 --> 00:01:05,060
So let's say that for some reason we wanted to read all of the books in the array and we wanted to know
13
00:01:05,060 --> 00:01:07,820
how many pages we would have to read.
14
00:01:08,120 --> 00:01:16,950
So the solution is to add together all of these pages, properties of all of the books in the array.
15
00:01:17,470 --> 00:01:18,160
Right.
16
00:01:18,160 --> 00:01:21,700
And so for that we can use the reduce method.
17
00:01:22,850 --> 00:01:24,800
So let's say pages.
18
00:01:25,760 --> 00:01:26,270
All.
19
00:01:27,330 --> 00:01:30,960
Books and then books Dot.
20
00:01:31,880 --> 00:01:32,870
Reduce.
21
00:01:33,290 --> 00:01:39,200
Now, the reduce method also takes in a callback function which will be executed for each element of
22
00:01:39,200 --> 00:01:39,740
the array.
23
00:01:39,740 --> 00:01:45,920
And as the second argument, it also takes in a starter value basically.
24
00:01:45,920 --> 00:01:52,160
But before we go into these, let's actually understand what the reduce method does and what it's used
25
00:01:52,160 --> 00:01:52,700
for.
26
00:01:53,260 --> 00:02:01,810
So it is called the reduce method, because the goal of reduce is to reduce basically the entire array
27
00:02:01,810 --> 00:02:03,320
to just one value.
28
00:02:03,340 --> 00:02:09,660
So to boil it down, and if you think about it, this is exactly what we are trying to do now.
29
00:02:09,670 --> 00:02:17,020
So we want to boil down the entire array to just one number, which is in this case, the number of
30
00:02:17,020 --> 00:02:19,750
pages of all the books combined.
31
00:02:19,780 --> 00:02:20,620
Right?
32
00:02:20,620 --> 00:02:27,610
And so that second argument that I mentioned earlier is basically the starter value for that final value
33
00:02:27,610 --> 00:02:29,140
that we want to obtain.
34
00:02:29,290 --> 00:02:32,320
So first, we will want to pass in a function.
35
00:02:32,320 --> 00:02:33,880
We will do that in a minute.
36
00:02:34,210 --> 00:02:36,670
And second, we want that starter value.
37
00:02:36,670 --> 00:02:42,270
And in this case, since we are adding together all of these values, we will start from zero.
38
00:02:42,280 --> 00:02:44,950
Of course, it makes sense, right?
39
00:02:46,320 --> 00:02:50,400
And now here, let's talk about the function that will get called.
40
00:02:51,020 --> 00:02:59,140
So before we always had this callback function with a book, and then here we did something right.
41
00:02:59,150 --> 00:03:02,510
But here the callback function will be a little bit different.
42
00:03:02,630 --> 00:03:09,020
So the function that we pass in here will not only get called with the current element, but also with
43
00:03:09,020 --> 00:03:13,850
the current value of the accumulator, which starts at zero.
44
00:03:13,970 --> 00:03:21,980
So that's all sounds very confusing, but let me just call this here ACC, which stands for accumulator.
45
00:03:21,980 --> 00:03:25,130
And then we will see in a second why this makes sense.
46
00:03:25,710 --> 00:03:32,790
So here what we want to return is this accumulator plus book dot.
47
00:03:33,570 --> 00:03:34,320
Pages.
48
00:03:35,170 --> 00:03:39,640
And now if we say pages, all books, then we get this huge number.
49
00:03:40,690 --> 00:03:46,360
Okay, so let's understand what happened here by going over this array element by element.
50
00:03:46,630 --> 00:03:50,040
So the first element again, here is the Lord of the Rings.
51
00:03:50,050 --> 00:03:54,660
And so in this first iteration, this book here is of course, this one.
52
00:03:54,670 --> 00:03:56,680
But what is the accumulator?
53
00:03:56,830 --> 00:04:04,780
Well, the accumulator is basically the current value of the final value that we want to boil the array
54
00:04:04,810 --> 00:04:05,950
down to.
55
00:04:06,280 --> 00:04:12,970
So basically, in this case, the pages of all the books combined, which starts at zero.
56
00:04:12,970 --> 00:04:17,220
And so in the first iteration, the accumulator will still be zero.
57
00:04:17,230 --> 00:04:24,430
So this function here will get called for the first element and then the accumulator will be zero and
58
00:04:24,430 --> 00:04:26,190
the book will be the current book.
59
00:04:26,200 --> 00:04:33,070
And so then we add together the current accumulator, so the current pages, all books, so to say,
60
00:04:33,070 --> 00:04:37,510
which is still zero plus this 1216.
61
00:04:37,780 --> 00:04:45,560
And so we can imagine that the second value here then becomes 1216 after this first iteration.
62
00:04:45,710 --> 00:04:52,040
And so therefore, in the next iteration, when we go to the second object, the current accumulator
63
00:04:52,040 --> 00:04:54,590
is already 1216.
64
00:04:55,860 --> 00:05:00,720
So here again, it will in the second iteration, be already 1216.
65
00:05:00,720 --> 00:05:04,410
And then on top of that we add 295.
66
00:05:04,440 --> 00:05:06,840
So that's then the current book pages.
67
00:05:06,840 --> 00:05:16,160
And so then in the next iteration, the accumulator will already be 1216 plus 295, which is like 1500
68
00:05:16,170 --> 00:05:16,710
something.
69
00:05:16,710 --> 00:05:23,100
And then on top of that, we add this number of pages and so it goes all the way until the end.
70
00:05:23,430 --> 00:05:30,450
So we can imagine this accumulator here basically as a pile onto which we put more and more and more
71
00:05:30,450 --> 00:05:34,410
until in the end we reach the final result that we were looking for.
72
00:05:34,410 --> 00:05:41,430
So it's like an intermediary value in this case onto which we keep adding the number of pages until
73
00:05:41,430 --> 00:05:45,600
in the end we reach 3227 pages.
74
00:05:46,720 --> 00:05:54,910
Okay, so that was a bit confusing once again, but it's also not super important to understand how
75
00:05:54,910 --> 00:06:01,660
it works, especially if all we're doing is like this simple adding of numbers.
76
00:06:01,660 --> 00:06:04,030
So this always works the same in the end.
77
00:06:04,030 --> 00:06:09,640
And so the most important use case usually of reduce is just this.
78
00:06:09,640 --> 00:06:16,210
But we can also do very, very complex things, as I said in the beginning, because this initial value
79
00:06:16,210 --> 00:06:18,910
here doesn't have to be a number.
80
00:06:18,910 --> 00:06:22,090
It could also be an array or it could be an object.
81
00:06:22,240 --> 00:06:29,920
And so what this means is that we could do really anything that we do with map or filter, also with
82
00:06:29,920 --> 00:06:30,730
a reduce.
83
00:06:30,910 --> 00:06:37,690
But I won't go into that right now because yeah, for that we have already map and filter.
84
00:06:38,200 --> 00:06:43,870
Now here I just like to in this case to replace the accumulator value with some.
85
00:06:45,610 --> 00:06:49,690
So calling it just some because that is what it's actually doing.
86
00:06:49,690 --> 00:06:57,130
But just keep in mind that, well, it is an accumulator because we keep adding up and up and up onto
87
00:06:57,130 --> 00:06:59,590
this temporary value, so to say.
88
00:06:59,590 --> 00:07:01,930
In this case, it's simply the sum of the pages.
89
00:07:01,960 --> 00:07:05,380
And so here I like to then call it sum.
90
00:07:06,740 --> 00:07:07,190
Okay.
91
00:07:07,190 --> 00:07:13,520
And that's actually all I had to show you here about reduce because, yeah, many times we just use
92
00:07:13,520 --> 00:07:15,230
it for simple stuff like this.
93
00:07:15,410 --> 00:07:18,140
And so now you know how to do that.
9189
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.