1
00:00:06,460 --> 00:00:07,810
In this video.

2
00:00:07,840 --> 00:00:14,740
Let's continue our array methods and we are going to talk about filter method.

3
00:00:14,920 --> 00:00:22,780
Well, filter method is one of my favorite methods when it comes to array because with this method we

4
00:00:22,780 --> 00:00:27,490
can achieve a complex logic with few lines of code.

5
00:00:27,520 --> 00:00:33,650
So let's say that we have a list of students, so let's bring back our students array here.

6
00:00:33,670 --> 00:00:35,380
Think it's going fast.

7
00:00:35,380 --> 00:00:41,620
So let me copy here and comment this one out and now let's bring it inside the filter method.

8
00:00:42,370 --> 00:00:48,040
So for these students, let's say that we want to know which student owes the school.

9
00:00:48,070 --> 00:00:56,230
Maybe you have a debit to be paid so we can use filter to filter out all those students who are owing

10
00:00:56,230 --> 00:00:56,980
the school.

11
00:00:57,340 --> 00:00:58,780
So in that case.

12
00:01:00,010 --> 00:01:05,620
So in this case, let's add additional property code debt, meaning you are owing the school.

13
00:01:05,740 --> 00:01:09,460
So let's say that the first student indeed you are in the school.

14
00:01:09,490 --> 00:01:17,200
So here we can copy this one and paste it here and then paste it here and also paste it here.

15
00:01:17,560 --> 00:01:19,020
The first one is Mary.

16
00:01:19,030 --> 00:01:22,630
Listen, that Mary is not in the school, so it's going to be first.

17
00:01:22,750 --> 00:01:24,440
Likewise, Sally.

18
00:01:24,460 --> 00:01:28,690
So Sally also is not only in the school, so forth.

19
00:01:28,810 --> 00:01:35,440
So for this kind of record, because we have few data here, when I ask you that, please, can you

20
00:01:35,440 --> 00:01:42,070
print out all those students who are not owing the school because we have only four records?

21
00:01:42,100 --> 00:01:44,300
You can say, yes, this is pretty easy.

22
00:01:44,320 --> 00:01:45,220
I'll look through.

23
00:01:45,220 --> 00:01:50,410
The first one is Mary, depth is first and then Sally also depth is false here.

24
00:01:50,410 --> 00:01:54,610
So it means that Mary and Sally are not all in the school.

25
00:01:55,090 --> 00:01:57,970
So I can just print out those names easily.

26
00:01:58,480 --> 00:02:00,610
So what about if you have a bunch of students?

27
00:02:00,610 --> 00:02:02,090
How are you going to do it?

28
00:02:02,110 --> 00:02:04,690
That's why Filter comes into play.

29
00:02:04,720 --> 00:02:08,830
So with Filter, we can do a lot of logic inside our code.

30
00:02:08,830 --> 00:02:16,000
For example, we can calculate which student age is more than 20 years or which students age is less

31
00:02:16,000 --> 00:02:17,080
than 23 years.

32
00:02:17,200 --> 00:02:21,210
So because of filter, we can achieve this one with few lines of code.

33
00:02:21,220 --> 00:02:24,820
So let's see the same syntax for all the array.

34
00:02:24,830 --> 00:02:33,070
So students do not filter and all these are high order function so we can pass a callback function as

35
00:02:33,070 --> 00:02:33,550
that.

36
00:02:33,670 --> 00:02:37,480
So for this one is also take in all three arguments.

37
00:02:37,480 --> 00:02:40,390
But what we came with here is an actual student.

38
00:02:40,390 --> 00:02:44,350
So here my error function and there we go.

39
00:02:44,680 --> 00:02:51,430
So for this one, we have access to the individual student as STD or student for short.

40
00:02:52,000 --> 00:02:57,040
So for this one filter also create new array as map.

41
00:02:57,280 --> 00:03:02,620
But with filter we can mix some logic before returning it, unlike map.

42
00:03:02,800 --> 00:03:04,240
So we see differences now.

43
00:03:04,240 --> 00:03:10,510
So let's check it out and see how can we retain all students who are all in the school?

44
00:03:10,810 --> 00:03:11,380
Perfect.

45
00:03:11,380 --> 00:03:14,200
So for this implementation, here we go.

46
00:03:14,230 --> 00:03:15,310
It's pretty simple, right?

47
00:03:15,310 --> 00:03:17,530
So you bring in return.

48
00:03:18,190 --> 00:03:20,920
And for return we have the student.

49
00:03:20,920 --> 00:03:23,920
This one, there is nothing new until this point.

50
00:03:24,250 --> 00:03:28,030
So let's assign a value core.

51
00:03:28,270 --> 00:03:33,190
Let me say student new student let me say dead.

52
00:03:33,190 --> 00:03:35,620
Student Then you put in that way.

53
00:03:36,730 --> 00:03:38,590
Okay, so equal to that.

54
00:03:38,590 --> 00:03:47,110
So if I console.log the dead student, meaning those who are only school, if I console.log quizzes

55
00:03:47,110 --> 00:03:51,100
that I see all the false students being displayed.

56
00:03:51,100 --> 00:03:54,970
Whether you are own or not, your name is still here.

57
00:03:55,060 --> 00:04:02,640
So the cool thing about Filter is that we can implement our own logic before returning the data.

58
00:04:02,650 --> 00:04:13,000
So what I'm going to do is that if student dot depth is equal to true meaning, those students are own.

59
00:04:13,000 --> 00:04:15,190
So when I save it, let's see.

60
00:04:15,190 --> 00:04:21,959
I'm going to have two students only instead of four, because these two students are having depth.

61
00:04:21,970 --> 00:04:25,360
That is why we see depth depth here, which is equal to true.

62
00:04:26,140 --> 00:04:33,580
So now let's say that if you change all this one to force, it means that no student is worth over in

63
00:04:33,580 --> 00:04:34,120
the school.

64
00:04:34,150 --> 00:04:41,020
So let's see, you're going to have empty array because for this one, there is no student who is or

65
00:04:41,020 --> 00:04:42,610
who owned the school.

66
00:04:42,850 --> 00:04:45,820
So let's bring back two students in the mix.

67
00:04:45,820 --> 00:04:48,460
So these two students are already in the school.

68
00:04:48,490 --> 00:04:52,980
So you can see that now we have two students in the array as that.

69
00:04:52,980 --> 00:04:55,600
That's a beauty about water.

70
00:04:55,630 --> 00:05:01,810
So we can also calculate which student age is more than 20 years or 30 years.

71
00:05:01,840 --> 00:05:04,000
I think you can try this on your own.

72
00:05:04,150 --> 00:05:09,670
So for this particular syntax tool, we can simplify it because we are using a function.

73
00:05:09,850 --> 00:05:16,090
So because we are just retaining a statement, we can even ignore the codebases and remove this one

74
00:05:16,090 --> 00:05:20,800
and remove the return statement and I remove this one.

75
00:05:20,920 --> 00:05:25,150
And when I save it, yes, this is perfect syntax for error function.

76
00:05:25,150 --> 00:05:30,790
So if this function is to confuse this, you stick to normal function like this one and you'll be good

77
00:05:30,790 --> 00:05:32,380
to go the same process.

78
00:05:32,380 --> 00:05:37,210
So what about, like I said, let's find all students whose names is John.

79
00:05:37,210 --> 00:05:38,140
We can do it.

80
00:05:38,140 --> 00:05:41,350
What about our students whose age is less than 20 years?

81
00:05:41,350 --> 00:05:43,270
So we can do that quickly.

82
00:05:43,270 --> 00:05:52,180
So here, just bring in the here and let's see the age, which is less than 20.

83
00:05:52,180 --> 00:05:55,120
Let's see, 30 years if I save it.

84
00:05:55,120 --> 00:05:55,840
Let's see.

85
00:05:55,840 --> 00:05:57,670
We have all the four students.

86
00:05:57,670 --> 00:05:59,410
Because all the four students.

87
00:05:59,520 --> 00:06:01,690
Age less than 30 years.

88
00:06:01,710 --> 00:06:09,640
So what about if you make use of less than 26 years old?

89
00:06:09,660 --> 00:06:16,470
Let's say you're going to have three students because looking at their ages, 20, 22 and 23 are less

90
00:06:16,470 --> 00:06:17,190
than 30.

91
00:06:17,190 --> 00:06:17,760
26.

92
00:06:17,760 --> 00:06:20,820
Yes, that is it about theater.

93
00:06:21,480 --> 00:06:23,820
Again, you are going to make use of this a lot.

94
00:06:23,820 --> 00:06:30,750
So let's go rockin the next video and talk about another important method called Reduced.

