Narrator: Now let's learn about the Filter Method.
Which, as we learned before is used to filter for elements
that satisfy a certain condition.
And how do we specify such a condition?
Well, you guessed it, we use a callback function again.
So, let's again work with our movements.
And so now the filter array.
And once again I will start with a regular function.
And just like the other callback functions like in the Map,
and for each, this one also gets access
to the current array element as well as the index
and the entire array.
But let's only work with the current element here,
which in filter is usually all that we need.
Okay, now what we want to do here
is to create an array of the deposits.
So let's actually write that.
So deposits and deposits are only the movements
that are above zero.
So we already know that. Right?
Let's indeed take a look at the movements here.
So here they are.
And so we want to filter out these negative values.
And so that condition is so that only the values
that has the condition will then make it into the new array.
And passing that condition means that it's true.
So, we will again return something here.
And here, the trick is that we return a Boolean value.
And so, in this case
all we need to do is to say the current movement,
greater than zero and that's it.
And so now only the array elements,
for which this condition is true
will make it into the deposits array.
All the other ones will simply get filtered out.
So they will not be included in this deposits array.
Okay.
And in fact, we only get the positive values now.
Beautiful.
And that's actually it, that's really simple, right?
So, just as I told you if you know how to use
the callback function correctly
and if you know how and why it works,
then all of these methods, become really really simple.
And once more, I also want to use a four of loop here again
so that you can appreciate the difference.
And that's very simple.
So, we need of course the same condition.
So if the current movement is greater than zero,
then we want to push the current element into a new array.
So we need to create that here deposits
for and start again with an empty array.
And so in this case, we simply push it there.
So we push the current movement.
And so let's take a look now.
And, as expected we get the same result.
And so you could again ask, what is the big difference here?
Like, what's the big deal
and why not just use the four loop for everything.
And the reason for that is again the push that exists
in JavaScript for using more functional code, like this.
But there's also a more practical implication here.
And that's because we can actually chain
all of these methods together.
So, basically use them all one after another
to build a big final result.
So, a bit similar in fact to what we did here
in our application, right here.
But here we mixed string methods with array methods,
but later on we will do, like big chains,
only with array methods
and that would be completely impossible using the four loop.
So, that's another big advantage of using the methods
instead of the regular four loop.
Now, anyway I have just another small challenge
for you here which is to create an array of the withdrawals.
So, withdrawals like this.
So I want you to create this array without looking
at the code we already wrote.
And so the withdrawals should only include
the negative numbers too.
So pause the video now for a minute,
and please try to write this code on your own.
Okay, so hopefully you did that.
And so, it is movements and again dot filter.
And now I will use an arrow function here.
So I will leave this one with the regular one
and now an arrow function.
Okay. And now, more of or less than zero, and that's it.
This is our condition and never forget
that this one is actually returned here.
So again, it is like having return written here.
And so this is why this works
lets log it to the console
and yeah, only the negative numbers remain now.
All the positive ones get the result of false
here in this condition.
And so therefore, they were not included
in the withdrawals array.
And again, these callback function here also get access
to the current index and the whole array.
And I will just leave it here,
but I never used these in my life ever in the Filter Method.
Okay, so this was the second
of the data transformation methods.
So that's Map, Filter and Reduce.
And so, in the next video we will then talk about Reduce.