Welcome back.
In this video, let's continue our array methods and we are going to talk about reduce method.
Why?
Before we explain, let's have a look at the data structure that we are going to work on.
The first product is banana with price of $1, likewise orange, apple and pear.
So what is reduced function where for this one to we have covered it before.
So reduce function or method.
It is used to compress an array into a single value, meaning we have reduced the entire array into
a single value.
So most cases we use reduce for calculations.
So let's say that for this particular array, if I ask you that place, can you find me ordered to a
top price for the product we have in store?
Well, we can do that one by adding the prices together.
Right.
But assuming we have a bunch of products, how are we going to do.
That's why reduce its shine.
So let's see how you're going to use reduce you bring in our product as that and then we use method
as a function call.
So for this we this method is very special.
All the methods takes in only one element as a callback buffer reduce.
It says no, you need to give me two arguments.
One is a callback function and then the nets are going to be the initialize, meaning where you want
to start to add up the value together.
So here we go.
Let's use normal function here and I'll start a second argument comma and then what you are initialized
to.
So let's give it zero because you want to add up from zero, right?
So inside this callback to we have access to three arguments that we can access.
So here we can say accumulator or the current value.
So let's say that for this one we have total.
It can be anything here.
The next one here are going to be the actual student in the list or products.
Sorry, going to be the product itself.
So here is going to be the individual product product.
So inside my callback function here, let's try to console.log those values.
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
that by default we have zero because we begin at what zero.
That's why we have four zero because for each loop we are not doing any logic but instead just console.log
in total here.
So now we know that the total represents our total value in our array.
So what about let's check the product too.
We're going to have the individual product here.
So what we can do here is that first we are going to take the total here, which is zero, and then
on each loop you want to add the zero to the price inside the array upon each loop.
So first loop is going to add 0 to 2.
Now the total has become two on a first loop, second loop is two.
Then you're going to add 3 to 8.
That is second loop.
Now become five on third loop.
We are going to add this one plus four, which gives us nine.
So here we can return those values.
We know that the total represents the zero here at the first loop or before the loop runs.
So not going to be our tutor.
And remember on the product is an object.
So I want the price, only this price.
So come here, I'm going to be the product dot price.
So for this one is product p r for product.
Yeah.
So this one here like that.
So for this one, we can assign to a variable and say sum of products or is it price.
It can be price or product, some product price or key product.
This is some price.
This is some whatever.
Yeah.
So here if I cancel look sum or total, we're going to see that we have ten.