The next that I want to introduce you are filter and fight.
I decided to describe both of them in one video because they work in similar and simple way.
So I don't want to consider them separately.
All right.
Let's write some examples.
I'm going to start with the filter holper.
For that, I want to create an array of persons which will consist of several objects and suppose that
we want to filter these objects depending on some condition.
So that's right, verbal person's.
That insert here an object with property name Mary and then right, another property, gender female,
let's duplicate it three more times and change names and genders properly.
Right, John?
And gender male, then, Jane.
That Bob and gender male, OK, as we said, we want to filter those objects and suppose that we want
to choose only male objects and add them into a new era for that.
At first, we need to create a new area.
Let's call it males.
Then we need to use for a loop in order to iterate through the items, choose the proper one and using
push method, add it to new right, create for a loop by its variable equal to zero.
Then the condition is less than the length of a person's.
Then I plus plus.
Here, we're going to use your statement and as a condition, we need to check if gender of the person
is equal to male rights if persons.
I doubt gender equals to male.
So if this condition is true, then we need to write mail, start, push.
And as a parameter persons' I.
So let's lock them in council writes the blog Persons'.
And then cancel the log mails.
Reload and here we have at first original persons are right, and then the filtered one, which consists
of male persons.
All right, this is the way which uses follow up, and I'm going to do the same using Filter Harper,
but before that, let's again separate the codes.
I'm going to create variable females, add in this variable, I'm going to store this case female persons,
so create variable females, which equals two persons.
Use your filter helper, the syntax again is similar as it was in case of previous helpers.
We need to pass your anonymous function with the parameter person, which again will be in singular
form.
So, as we said, we want to choose female persons, therefore we need to write a written statement
in which the gender property should be equal to female white return person, not gender.
Two people equals two female.
Let's again look both variables in council, right, castle log persons.
And that costs a lot of females.
Reload and you see that at first we have, again, original persons are right and then filtered Arah,
which consists of female persons.
So Filter Method did absolutely the same job as follow.
But again, you see at a glance that we needed for that less time because of less amount of code.
And again, logic is very simple and easy to understand in case of filter hopper.
All right, let's take a look in depth how filter hopper actually works behind the scenes.
So here we have our persons array with it's containing objects.
Then we have the variable females in which female persons should be added.
And also here we have a filter hopper on first iteration function is executed for the first object with
no property marry.
Because of that, Mary has the gender female.
The condition inside function is true.
Therefore it returns to value and the object is added to female Arite.
The function is executed for the second object, which has name property John and the gender male condition
is false.
So function returns false value and therefore object is not added to a right.
Then in the same way function is executed for the object.
Jane condition is through.
An object is added to a right.
As for the object, you see that gender is male function, which false value because condition is false.
Therefore object is not added to the right.
All right.
That's the way our filter Hartill actually works.
Let's go to Brackett's and move on to the next helper, which is find, as we said, find works in the
similar way as filter householder's.
But there is only one difference.
Filter Holper iterate through of the items in array and adds enumerate the item for which the condition
is true.
But in case of found helper, when it finds that condition is true, it adds this current item in new
array and immediately terminates iteration.
Makes sense.
OK, so if we change here, filter into find and then reload, we'll will get onto the first object
because the condition for the first item is through and find out how it stops.
Iteration, we can reach the same result if we use followup for that.
We just need to pass your known break statement, which actually stops looping through the items.
So right.
Break, then reload.
And you see that we have the same result.
OK, let's consider briefly how found Harper works.
Again, we have here our person story that the females variable and find helper, so are the first iteration
function is executed for the first item, which is an object within the property and gender female.
The condition inside function is true.
It returns true.
Value object is added to females array and after that helper terminates working.
So this is the way I'll find help actually works behind the scenes.
All right, that's it.
See you in the next lecture.