WEBVTT

00:01.370 --> 00:05.300
The next that I want to introduce you are filter and fight.

00:05.970 --> 00:11.330
I decided to describe both of them in one video because they work in similar and simple way.

00:11.690 --> 00:14.660
So I don't want to consider them separately.

00:15.020 --> 00:15.530
All right.

00:16.040 --> 00:17.790
Let's write some examples.

00:17.810 --> 00:20.000
I'm going to start with the filter holper.

00:20.240 --> 00:26.480
For that, I want to create an array of persons which will consist of several objects and suppose that

00:26.480 --> 00:29.240
we want to filter these objects depending on some condition.

00:30.130 --> 00:33.250
So that's right, verbal person's.

00:34.750 --> 00:46.750
That insert here an object with property name Mary and then right, another property, gender female,

00:48.230 --> 00:53.010
let's duplicate it three more times and change names and genders properly.

00:53.020 --> 00:54.840
Right, John?

00:55.720 --> 00:59.620
And gender male, then, Jane.

01:02.830 --> 01:12.520
That Bob and gender male, OK, as we said, we want to filter those objects and suppose that we want

01:12.520 --> 01:17.400
to choose only male objects and add them into a new era for that.

01:17.410 --> 01:19.540
At first, we need to create a new area.

01:20.350 --> 01:21.720
Let's call it males.

01:22.750 --> 01:28.210
Then we need to use for a loop in order to iterate through the items, choose the proper one and using

01:28.210 --> 01:34.780
push method, add it to new right, create for a loop by its variable equal to zero.

01:35.440 --> 01:39.340
Then the condition is less than the length of a person's.

01:40.440 --> 01:41.610
Then I plus plus.

01:43.360 --> 01:49.180
Here, we're going to use your statement and as a condition, we need to check if gender of the person

01:49.180 --> 01:53.230
is equal to male rights if persons.

01:54.630 --> 02:00.270
I doubt gender equals to male.

02:03.200 --> 02:08.660
So if this condition is true, then we need to write mail, start, push.

02:10.550 --> 02:14.390
And as a parameter persons' I.

02:17.220 --> 02:21.330
So let's lock them in council writes the blog Persons'.

02:23.810 --> 02:25.940
And then cancel the log mails.

02:29.010 --> 02:36.240
Reload and here we have at first original persons are right, and then the filtered one, which consists

02:36.240 --> 02:37.320
of male persons.

02:38.460 --> 02:44.060
All right, this is the way which uses follow up, and I'm going to do the same using Filter Harper,

02:44.490 --> 02:47.730
but before that, let's again separate the codes.

02:51.100 --> 02:57.790
I'm going to create variable females, add in this variable, I'm going to store this case female persons,

02:58.210 --> 03:02.530
so create variable females, which equals two persons.

03:03.780 --> 03:09.340
Use your filter helper, the syntax again is similar as it was in case of previous helpers.

03:09.840 --> 03:14.850
We need to pass your anonymous function with the parameter person, which again will be in singular

03:14.850 --> 03:15.180
form.

03:17.460 --> 03:22.710
So, as we said, we want to choose female persons, therefore we need to write a written statement

03:23.040 --> 03:30.510
in which the gender property should be equal to female white return person, not gender.

03:32.320 --> 03:33.940
Two people equals two female.

03:36.700 --> 03:41.470
Let's again look both variables in council, right, castle log persons.

03:44.380 --> 03:46.570
And that costs a lot of females.

03:49.950 --> 03:56.100
Reload and you see that at first we have, again, original persons are right and then filtered Arah,

03:56.100 --> 03:58.440
which consists of female persons.

03:59.400 --> 04:02.640
So Filter Method did absolutely the same job as follow.

04:02.660 --> 04:09.000
But again, you see at a glance that we needed for that less time because of less amount of code.

04:09.240 --> 04:14.130
And again, logic is very simple and easy to understand in case of filter hopper.

04:14.970 --> 04:19.710
All right, let's take a look in depth how filter hopper actually works behind the scenes.

04:20.160 --> 04:23.520
So here we have our persons array with it's containing objects.

04:24.150 --> 04:28.290
Then we have the variable females in which female persons should be added.

04:28.740 --> 04:35.880
And also here we have a filter hopper on first iteration function is executed for the first object with

04:35.880 --> 04:36.930
no property marry.

04:37.560 --> 04:39.830
Because of that, Mary has the gender female.

04:39.870 --> 04:41.900
The condition inside function is true.

04:42.360 --> 04:46.020
Therefore it returns to value and the object is added to female Arite.

04:46.980 --> 04:53.850
The function is executed for the second object, which has name property John and the gender male condition

04:53.850 --> 04:54.460
is false.

04:54.930 --> 04:59.160
So function returns false value and therefore object is not added to a right.

05:00.120 --> 05:03.810
Then in the same way function is executed for the object.

05:03.810 --> 05:05.850
Jane condition is through.

05:05.850 --> 05:07.430
An object is added to a right.

05:08.160 --> 05:14.910
As for the object, you see that gender is male function, which false value because condition is false.

05:15.310 --> 05:17.460
Therefore object is not added to the right.

05:18.690 --> 05:19.140
All right.

05:19.530 --> 05:22.080
That's the way our filter Hartill actually works.

05:22.810 --> 05:28.710
Let's go to Brackett's and move on to the next helper, which is find, as we said, find works in the

05:28.710 --> 05:30.660
similar way as filter householder's.

05:31.140 --> 05:32.940
But there is only one difference.

05:33.480 --> 05:41.160
Filter Holper iterate through of the items in array and adds enumerate the item for which the condition

05:41.160 --> 05:41.580
is true.

05:42.120 --> 05:48.840
But in case of found helper, when it finds that condition is true, it adds this current item in new

05:48.840 --> 05:51.740
array and immediately terminates iteration.

05:52.980 --> 05:53.670
Makes sense.

05:54.800 --> 06:03.320
OK, so if we change here, filter into find and then reload, we'll will get onto the first object

06:03.320 --> 06:07.070
because the condition for the first item is through and find out how it stops.

06:07.070 --> 06:12.020
Iteration, we can reach the same result if we use followup for that.

06:12.020 --> 06:17.570
We just need to pass your known break statement, which actually stops looping through the items.

06:17.840 --> 06:18.340
So right.

06:18.350 --> 06:21.170
Break, then reload.

06:21.740 --> 06:23.560
And you see that we have the same result.

06:24.650 --> 06:28.250
OK, let's consider briefly how found Harper works.

06:29.060 --> 06:37.340
Again, we have here our person story that the females variable and find helper, so are the first iteration

06:37.340 --> 06:43.670
function is executed for the first item, which is an object within the property and gender female.

06:44.270 --> 06:46.130
The condition inside function is true.

06:46.730 --> 06:47.630
It returns true.

06:47.630 --> 06:54.020
Value object is added to females array and after that helper terminates working.

06:55.570 --> 06:59.080
So this is the way I'll find help actually works behind the scenes.

07:00.280 --> 07:01.960
All right, that's it.

07:02.200 --> 07:03.450
See you in the next lecture.
