In this video.
Let's continue our array methods and we are going to talk about filter method.
Well, filter method is one of my favorite methods when it comes to array because with this method we
can achieve a complex logic with few lines of code.
So let's say that we have a list of students, so let's bring back our students array here.
Think it's going fast.
So let me copy here and comment this one out and now let's bring it inside the filter method.
So for these students, let's say that we want to know which student owes the school.
Maybe you have a debit to be paid so we can use filter to filter out all those students who are owing
the school.
So in that case.
So in this case, let's add additional property code debt, meaning you are owing the school.
So let's say that the first student indeed you are in the school.
So here we can copy this one and paste it here and then paste it here and also paste it here.
The first one is Mary.
Listen, that Mary is not in the school, so it's going to be first.
Likewise, Sally.
So Sally also is not only in the school, so forth.
So for this kind of record, because we have few data here, when I ask you that, please, can you
print out all those students who are not owing the school because we have only four records?
You can say, yes, this is pretty easy.
I'll look through.
The first one is Mary, depth is first and then Sally also depth is false here.
So it means that Mary and Sally are not all in the school.
So I can just print out those names easily.
So what about if you have a bunch of students?
How are you going to do it?
That's why Filter comes into play.
So with Filter, we can do a lot of logic inside our code.
For example, we can calculate which student age is more than 20 years or which students age is less
than 23 years.
So because of filter, we can achieve this one with few lines of code.
So let's see the same syntax for all the array.
So students do not filter and all these are high order function so we can pass a callback function as
that.
So for this one is also take in all three arguments.
But what we came with here is an actual student.
So here my error function and there we go.
So for this one, we have access to the individual student as STD or student for short.
So for this one filter also create new array as map.
But with filter we can mix some logic before returning it, unlike map.
So we see differences now.
So let's check it out and see how can we retain all students who are all in the school?
Perfect.
So for this implementation, here we go.
It's pretty simple, right?
So you bring in return.
And for return we have the student.
This one, there is nothing new until this point.
So let's assign a value core.
Let me say student new student let me say dead.
Student Then you put in that way.
Okay, so equal to that.
So if I console.log the dead student, meaning those who are only school, if I console.log quizzes
that I see all the false students being displayed.
Whether you are own or not, your name is still here.
So the cool thing about Filter is that we can implement our own logic before returning the data.
So what I'm going to do is that if student dot depth is equal to true meaning, those students are own.
So when I save it, let's see.
I'm going to have two students only instead of four, because these two students are having depth.
That is why we see depth depth here, which is equal to true.
So now let's say that if you change all this one to force, it means that no student is worth over in
the school.
So let's see, you're going to have empty array because for this one, there is no student who is or
who owned the school.
So let's bring back two students in the mix.
So these two students are already in the school.
So you can see that now we have two students in the array as that.
That's a beauty about water.
So we can also calculate which student age is more than 20 years or 30 years.
I think you can try this on your own.
So for this particular syntax tool, we can simplify it because we are using a function.
So because we are just retaining a statement, we can even ignore the codebases and remove this one
and remove the return statement and I remove this one.
And when I save it, yes, this is perfect syntax for error function.
So if this function is to confuse this, you stick to normal function like this one and you'll be good
to go the same process.
So what about, like I said, let's find all students whose names is John.
We can do it.
What about our students whose age is less than 20 years?
So we can do that quickly.
So here, just bring in the here and let's see the age, which is less than 20.
Let's see, 30 years if I save it.
Let's see.
We have all the four students.
Because all the four students.
Age less than 30 years.
So what about if you make use of less than 26 years old?
Let's say you're going to have three students because looking at their ages, 20, 22 and 23 are less
than 30.
26.
Yes, that is it about theater.
Again, you are going to make use of this a lot.
So let's go rockin the next video and talk about another important method called Reduced.