1
00:00:03,440 --> 00:00:04,580
Welcome back.

2
00:00:04,610 --> 00:00:12,440
In this video, let's continue our array methods and we are going to talk about reduce method.

3
00:00:12,560 --> 00:00:12,950
Why?

4
00:00:12,950 --> 00:00:18,260
Before we explain, let's have a look at the data structure that we are going to work on.

5
00:00:18,950 --> 00:00:24,920
The first product is banana with price of $1, likewise orange, apple and pear.

6
00:00:24,950 --> 00:00:30,770
So what is reduced function where for this one to we have covered it before.

7
00:00:30,800 --> 00:00:33,380
So reduce function or method.

8
00:00:33,410 --> 00:00:41,240
It is used to compress an array into a single value, meaning we have reduced the entire array into

9
00:00:41,240 --> 00:00:42,470
a single value.

10
00:00:42,470 --> 00:00:46,520
So most cases we use reduce for calculations.

11
00:00:46,520 --> 00:00:54,410
So let's say that for this particular array, if I ask you that place, can you find me ordered to a

12
00:00:54,410 --> 00:00:57,920
top price for the product we have in store?

13
00:00:57,950 --> 00:01:02,090
Well, we can do that one by adding the prices together.

14
00:01:02,090 --> 00:01:02,750
Right.

15
00:01:02,750 --> 00:01:07,130
But assuming we have a bunch of products, how are we going to do.

16
00:01:07,160 --> 00:01:09,770
That's why reduce its shine.

17
00:01:10,340 --> 00:01:18,230
So let's see how you're going to use reduce you bring in our product as that and then we use method

18
00:01:18,260 --> 00:01:19,670
as a function call.

19
00:01:19,880 --> 00:01:23,180
So for this we this method is very special.

20
00:01:23,300 --> 00:01:29,330
All the methods takes in only one element as a callback buffer reduce.

21
00:01:29,360 --> 00:01:32,750
It says no, you need to give me two arguments.

22
00:01:32,780 --> 00:01:38,180
One is a callback function and then the nets are going to be the initialize, meaning where you want

23
00:01:38,180 --> 00:01:40,610
to start to add up the value together.

24
00:01:40,610 --> 00:01:41,780
So here we go.

25
00:01:41,810 --> 00:01:49,420
Let's use normal function here and I'll start a second argument comma and then what you are initialized

26
00:01:49,420 --> 00:01:49,650
to.

27
00:01:50,090 --> 00:01:54,380
So let's give it zero because you want to add up from zero, right?

28
00:01:54,530 --> 00:02:00,560
So inside this callback to we have access to three arguments that we can access.

29
00:02:00,560 --> 00:02:04,280
So here we can say accumulator or the current value.

30
00:02:04,280 --> 00:02:06,770
So let's say that for this one we have total.

31
00:02:06,800 --> 00:02:08,360
It can be anything here.

32
00:02:08,389 --> 00:02:12,870
The next one here are going to be the actual student in the list or products.

33
00:02:12,870 --> 00:02:15,230
Sorry, going to be the product itself.

34
00:02:15,410 --> 00:02:22,730
So here is going to be the individual product product.

35
00:02:22,940 --> 00:02:28,700
So inside my callback function here, let's try to console.log those values.

36
00:02:28,700 --> 00:02:36,410
The tutor first and let's save it, let's save it and let's see what we have as a tutor that zero meaning

37
00:02:36,410 --> 00:02:41,570
that by default we have zero because we begin at what zero.

38
00:02:41,570 --> 00:02:48,890
That's why we have four zero because for each loop we are not doing any logic but instead just console.log

39
00:02:48,890 --> 00:02:49,850
in total here.

40
00:02:49,850 --> 00:02:54,530
So now we know that the total represents our total value in our array.

41
00:02:54,620 --> 00:02:57,300
So what about let's check the product too.

42
00:02:57,350 --> 00:02:59,630
We're going to have the individual product here.

43
00:03:00,020 --> 00:03:08,120
So what we can do here is that first we are going to take the total here, which is zero, and then

44
00:03:08,120 --> 00:03:15,110
on each loop you want to add the zero to the price inside the array upon each loop.

45
00:03:15,110 --> 00:03:19,610
So first loop is going to add 0 to 2.

46
00:03:19,700 --> 00:03:24,890
Now the total has become two on a first loop, second loop is two.

47
00:03:24,890 --> 00:03:26,870
Then you're going to add 3 to 8.

48
00:03:26,870 --> 00:03:27,980
That is second loop.

49
00:03:27,980 --> 00:03:30,230
Now become five on third loop.

50
00:03:30,230 --> 00:03:35,390
We are going to add this one plus four, which gives us nine.

51
00:03:35,390 --> 00:03:38,330
So here we can return those values.

52
00:03:38,330 --> 00:03:44,750
We know that the total represents the zero here at the first loop or before the loop runs.

53
00:03:44,750 --> 00:03:46,490
So not going to be our tutor.

54
00:03:46,490 --> 00:03:49,880
And remember on the product is an object.

55
00:03:49,880 --> 00:03:52,940
So I want the price, only this price.

56
00:03:52,940 --> 00:03:57,170
So come here, I'm going to be the product dot price.

57
00:03:57,170 --> 00:04:03,860
So for this one is product p r for product.

58
00:04:04,430 --> 00:04:04,820
Yeah.

59
00:04:04,820 --> 00:04:07,280
So this one here like that.

60
00:04:07,280 --> 00:04:15,830
So for this one, we can assign to a variable and say sum of products or is it price.

61
00:04:15,830 --> 00:04:21,019
It can be price or product, some product price or key product.

62
00:04:21,110 --> 00:04:22,370
This is some price.

63
00:04:22,400 --> 00:04:23,820
This is some whatever.

64
00:04:24,110 --> 00:04:24,530
Yeah.

65
00:04:24,530 --> 00:04:30,500
So here if I cancel look sum or total, we're going to see that we have ten.

