﻿1
00:00:01,260 --> 00:00:03,990
‫All right, so we tackled the map

2
00:00:03,990 --> 00:00:05,580
‫and the filter method

3
00:00:05,580 --> 00:00:08,340
‫and so, now, it's time for the one method

4
00:00:08,340 --> 00:00:12,453
‫that rules them all, which is the reduced method.

5
00:00:13,860 --> 00:00:17,100
‫So, the reduced method is really the most versatile

6
00:00:17,100 --> 00:00:22,080
‫and the most powerful of all array methods in JavaScript.

7
00:00:22,080 --> 00:00:24,540
‫So, in fact, we could, probably, implement

8
00:00:24,540 --> 00:00:28,710
‫all the other methods, simply using the reduced method,

9
00:00:28,710 --> 00:00:31,440
‫but that would be a little bit too much work.

10
00:00:31,440 --> 00:00:35,010
‫And so, that's why we have all of these other methods

11
00:00:35,010 --> 00:00:37,170
‫which are a lot easier to use.

12
00:00:37,170 --> 00:00:41,310
‫So, I will really not go deep into the reduced method, here,

13
00:00:41,310 --> 00:00:45,930
‫because we could spend hours just with this method,

14
00:00:45,930 --> 00:00:49,590
‫but I will just show you one of the most common use cases,

15
00:00:49,590 --> 00:00:52,380
‫which is to add together numbers

16
00:00:52,380 --> 00:00:55,560
‫or, basically, to perform any operation,

17
00:00:55,560 --> 00:00:58,530
‫or mathematical operations, with numbers.

18
00:00:58,530 --> 00:01:00,390
‫So, let's say that, for some reason,

19
00:01:00,390 --> 00:01:03,990
‫we wanted to read all of the books in the array

20
00:01:03,990 --> 00:01:08,490
‫and we wanted to know how many pages we would have to read.

21
00:01:08,490 --> 00:01:11,470
‫So, the solution is to add together

22
00:01:12,630 --> 00:01:14,820
‫all of these pages properties

23
00:01:14,820 --> 00:01:18,360
‫of all of the books in the array, right?

24
00:01:18,360 --> 00:01:21,693
‫And so, for that, we can use the reduce method.

25
00:01:23,010 --> 00:01:25,380
‫So, let's say pagesAllBooks,

26
00:01:29,104 --> 00:01:30,937
‫and then books.reduce.

27
00:01:33,600 --> 00:01:36,840
‫Now, the reduce method also takes in a callback function

28
00:01:36,840 --> 00:01:39,960
‫which will be executed for each element of the array.

29
00:01:39,960 --> 00:01:41,790
‫And, as a second argument,

30
00:01:41,790 --> 00:01:46,140
‫it also takes in a starter value, basically.

31
00:01:46,140 --> 00:01:47,790
‫But before we go into these,

32
00:01:47,790 --> 00:01:51,300
‫let's, actually, understand what the reduce method does

33
00:01:51,300 --> 00:01:53,490
‫and what it's used for.

34
00:01:53,490 --> 00:01:55,860
‫So, it is called the reduce method

35
00:01:55,860 --> 00:02:00,660
‫because the goal of reduce is to reduce, basically,

36
00:02:00,660 --> 00:02:03,510
‫the entire array to just one value.

37
00:02:03,510 --> 00:02:06,480
‫So, to boil it down, and if you think about it,

38
00:02:06,480 --> 00:02:09,990
‫this is exactly what we are trying to do, now.

39
00:02:09,990 --> 00:02:13,530
‫So, we want to boil down the entire array

40
00:02:13,530 --> 00:02:16,500
‫to just one number, which is, in this case,

41
00:02:16,500 --> 00:02:20,880
‫the number of pages of all the books combined, right?

42
00:02:20,880 --> 00:02:23,790
‫And so, that second argument that I mentioned earlier,

43
00:02:23,790 --> 00:02:26,010
‫is, basically, the starter value

44
00:02:26,010 --> 00:02:29,610
‫for that final value that we want to obtain.

45
00:02:29,610 --> 00:02:32,580
‫So first, we will want to pass in a function.

46
00:02:32,580 --> 00:02:34,159
‫We will do that in a minute.

47
00:02:34,159 --> 00:02:36,840
‫And second, we want that starter value.

48
00:02:36,840 --> 00:02:39,180
‫And in this case, since we are adding together

49
00:02:39,180 --> 00:02:42,720
‫all of these values, we will start from zero, of course.

50
00:02:42,720 --> 00:02:44,973
‫It makes sense, right?

51
00:02:46,560 --> 00:02:48,990
‫And now, here, let's talk about the function

52
00:02:48,990 --> 00:02:51,210
‫that will get called.

53
00:02:51,210 --> 00:02:56,210
‫So, before we always had this callback function with a book

54
00:02:56,850 --> 00:02:59,460
‫and then, here, we did something, right?

55
00:02:59,460 --> 00:03:00,900
‫But, here, the callback function

56
00:03:00,900 --> 00:03:02,970
‫will be a little bit different.

57
00:03:02,970 --> 00:03:04,830
‫So, the function that we pass in, here,

58
00:03:04,830 --> 00:03:08,130
‫will not only get caught with the current element

59
00:03:08,130 --> 00:03:11,760
‫but also with the current value of the accumulator

60
00:03:11,760 --> 00:03:14,280
‫which starts at zero.

61
00:03:14,280 --> 00:03:16,590
‫So, that all sounds very confusing,

62
00:03:16,590 --> 00:03:20,043
‫but let me just call this, here,

63
00:03:20,043 --> 00:03:22,200
‫acc, which stands for accumulator,

64
00:03:22,200 --> 00:03:25,920
‫and then, we will see, in a second, why this makes sense.

65
00:03:25,920 --> 00:03:30,000
‫So here, what we want to return is this accumulator

66
00:03:30,000 --> 00:03:34,323
‫plus book.pages.

67
00:03:35,370 --> 00:03:37,310
‫And now, if we say pages or books,

68
00:03:37,310 --> 00:03:39,633
‫then we get this huge number.

69
00:03:40,710 --> 00:03:43,500
‫Okay, so let's understand what happened, here,

70
00:03:43,500 --> 00:03:46,980
‫by going over this array, element by element.

71
00:03:46,980 --> 00:03:48,840
‫So, the first element, again, here,

72
00:03:48,840 --> 00:03:50,370
‫is "The Lord of the Rings."

73
00:03:50,370 --> 00:03:52,350
‫And so, in this first iteration,

74
00:03:52,350 --> 00:03:55,020
‫this book, here, is, of course, this one,

75
00:03:55,020 --> 00:03:57,120
‫but what is the accumulator?

76
00:03:57,120 --> 00:03:59,370
‫Well, the accumulator is, basically,

77
00:03:59,370 --> 00:04:02,640
‫the current value of the final value

78
00:04:02,640 --> 00:04:06,540
‫that we want to boil the array down to.

79
00:04:06,540 --> 00:04:08,550
‫So, basically, in this case,

80
00:04:08,550 --> 00:04:13,140
‫the pages of all the books combined, which starts at zero.

81
00:04:13,140 --> 00:04:14,730
‫And so, in the first iteration

82
00:04:14,730 --> 00:04:17,550
‫the accumulator will still be zero.

83
00:04:17,550 --> 00:04:19,770
‫So, this function, here, will get called

84
00:04:19,770 --> 00:04:24,360
‫for the first element and then the accumulator will be zero

85
00:04:24,360 --> 00:04:26,430
‫and the book will be the current book.

86
00:04:26,430 --> 00:04:29,580
‫And so, then we add together the current accumulator,

87
00:04:29,580 --> 00:04:33,270
‫so the current pages or books, so to say,

88
00:04:33,270 --> 00:04:38,070
‫which is still zero plus these 1,216.

89
00:04:38,070 --> 00:04:40,890
‫And so, we can imagine that the second value, here,

90
00:04:40,890 --> 00:04:45,890
‫then becomes 1,216 after this first iteration.

91
00:04:45,930 --> 00:04:48,360
‫And so, therefore, in the next iteration,

92
00:04:48,360 --> 00:04:50,790
‫when we go to the second object,

93
00:04:50,790 --> 00:04:54,423
‫the current accumulator is already 1,216.

94
00:04:56,040 --> 00:04:58,860
‫So here, again, it will, in the second iteration,

95
00:04:58,860 --> 00:05:00,930
‫be already 1,216.

96
00:05:00,930 --> 00:05:04,770
‫And then, on top of that, we add 295.

97
00:05:04,770 --> 00:05:07,200
‫So, that's then the current book pages.

98
00:05:07,200 --> 00:05:09,780
‫And so, then, in the next iteration

99
00:05:09,780 --> 00:05:14,700
‫the accumulator will already be 1,216 plus 295

100
00:05:14,700 --> 00:05:16,890
‫which is like 1,500 something.

101
00:05:16,890 --> 00:05:20,520
‫And then, on top of that, we add this number of pages,

102
00:05:20,520 --> 00:05:23,760
‫and so it goes, all the way, until the end.

103
00:05:23,760 --> 00:05:26,940
‫So, we can imagine this accumulator here, basically,

104
00:05:26,940 --> 00:05:30,630
‫as a pile, onto which we put more and more and more,

105
00:05:30,630 --> 00:05:33,180
‫until, in the end, we reach the final result

106
00:05:33,180 --> 00:05:34,680
‫that we were looking for.

107
00:05:34,680 --> 00:05:37,050
‫So, it's like an intermediary value,

108
00:05:37,050 --> 00:05:41,040
‫in this case, onto which we keep adding the number of pages

109
00:05:41,040 --> 00:05:45,573
‫until, in the end, we reach 3,227 pages.

110
00:05:46,800 --> 00:05:51,510
‫Okay, so that was a bit confusing, once again,

111
00:05:51,510 --> 00:05:53,910
‫but it's also not super important

112
00:05:53,910 --> 00:05:55,860
‫to understand how it works,

113
00:05:55,860 --> 00:05:58,170
‫especially if all we're doing

114
00:05:58,170 --> 00:06:01,890
‫is, like, this simple adding of numbers.

115
00:06:01,890 --> 00:06:04,320
‫So, this always works the same in the end.

116
00:06:04,320 --> 00:06:07,440
‫And so, yeah, the most important use case, usually,

117
00:06:07,440 --> 00:06:09,900
‫of reduce is just this.

118
00:06:09,900 --> 00:06:13,290
‫But we can also do very, very complex things,

119
00:06:13,290 --> 00:06:14,730
‫as I said, in the beginning,

120
00:06:14,730 --> 00:06:16,740
‫because this initial value, here,

121
00:06:16,740 --> 00:06:19,140
‫doesn't have to be a number.

122
00:06:19,140 --> 00:06:22,530
‫It could also be an array or it could be an object.

123
00:06:22,530 --> 00:06:24,360
‫And so, what this means

124
00:06:24,360 --> 00:06:26,040
‫is that we could do, really,

125
00:06:26,040 --> 00:06:28,500
‫anything that we do with map or filter

126
00:06:28,500 --> 00:06:31,230
‫also with a reduce.

127
00:06:31,230 --> 00:06:35,029
‫But I won't go into that, right now, because, yeah,

128
00:06:35,029 --> 00:06:38,310
‫(laughs) for that we have already map and filter.

129
00:06:38,310 --> 00:06:40,890
‫Now, here, I just like to, in this case,

130
00:06:40,890 --> 00:06:43,893
‫to replace the accumulator value with sum,

131
00:06:45,690 --> 00:06:46,980
‫so calling it just sum,

132
00:06:46,980 --> 00:06:50,010
‫because that is what it's actually doing.

133
00:06:50,010 --> 00:06:51,930
‫But just keep in mind that,

134
00:06:51,930 --> 00:06:53,730
‫well, it is an accumulator

135
00:06:53,730 --> 00:06:56,850
‫because we keep adding up and up and up

136
00:06:56,850 --> 00:06:59,850
‫onto this temporary value, so to say.

137
00:06:59,850 --> 00:07:02,130
‫In this case, it's simply the sum of the pages.

138
00:07:02,130 --> 00:07:05,403
‫And so, here, I like to then call it sum.

139
00:07:06,810 --> 00:07:09,930
‫Okay, and that's, actually, all I had to show you, here,

140
00:07:09,930 --> 00:07:11,250
‫about reduce,

141
00:07:11,250 --> 00:07:13,680
‫because, yeah, many times we just use it

142
00:07:13,680 --> 00:07:15,720
‫for simple stuff like this.

143
00:07:15,720 --> 00:07:18,123
‫And so, now, you know how to do that.

