Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,900 --> 00:00:07,320
Now, before you can start building any meaningful react application, you need to be a true master
2
00:00:07,320 --> 00:00:13,800
of the three functional array methods in JavaScript, which are map, filter and reduce.
3
00:00:13,890 --> 00:00:21,060
And I call them functional array methods because these methods do not mutate the original array, but
4
00:00:21,060 --> 00:00:25,350
do instead return a new array based on the original one.
5
00:00:25,500 --> 00:00:30,150
So in this lecture, let's start by taking a look at the map method.
6
00:00:31,800 --> 00:00:40,530
And to start, let's actually take out all this code here because we do no longer need it now, because
7
00:00:40,530 --> 00:00:45,180
from now on we will actually start working with the entire books array.
8
00:00:46,440 --> 00:00:49,640
So let me take that out, give it a save.
9
00:00:49,650 --> 00:00:55,830
And then here we have all this this panel and this output here cleaned up a bit.
10
00:00:56,600 --> 00:01:01,660
So let's again pretend that we are getting our books from this fictional API.
11
00:01:02,650 --> 00:01:05,050
Using the get books function.
12
00:01:05,990 --> 00:01:08,600
So let's say get books.
13
00:01:08,600 --> 00:01:13,190
And so remember that this will then give us the entire books array.
14
00:01:13,310 --> 00:01:17,750
And so now we are ready to use the map method on that array.
15
00:01:18,020 --> 00:01:20,990
So what does the map method actually do?
16
00:01:21,170 --> 00:01:27,890
Well, basically the map method will loop over an array and return a new array with the same length,
17
00:01:27,890 --> 00:01:33,050
with some operation applied to each of the elements of the original array.
18
00:01:34,300 --> 00:01:38,440
So just to illustrate that, let's say we have this very simple array.
19
00:01:38,650 --> 00:01:41,190
And on that we can call map.
20
00:01:41,200 --> 00:01:47,740
And so now Map expects a callback function, which is a function that will be called for each of these
21
00:01:47,740 --> 00:01:49,360
elements of the array.
22
00:01:49,930 --> 00:01:55,300
And so now here are these arrow functions that we talked about earlier become really handy because now
23
00:01:55,300 --> 00:01:58,900
we can simply use an arrow function here.
24
00:01:59,960 --> 00:02:01,370
For example, like this.
25
00:02:01,400 --> 00:02:03,790
And I will explain in a second what this does.
26
00:02:03,800 --> 00:02:06,770
But for now, let's just take a look at the output here.
27
00:02:08,060 --> 00:02:11,180
Actually, let's save this in some variable.
28
00:02:13,090 --> 00:02:20,260
And so what you see here is that we get an array which is basically double the previous array.
29
00:02:20,650 --> 00:02:22,840
So what happened here?
30
00:02:23,570 --> 00:02:30,050
So again, we passed in this callback function, which is a function that gets called for each of the
31
00:02:30,050 --> 00:02:31,730
elements of this array.
32
00:02:31,820 --> 00:02:38,420
And in this callback function, the element parameter will always be the current element of the array.
33
00:02:38,450 --> 00:02:42,680
So for this first element here, l will become one.
34
00:02:42,950 --> 00:02:47,240
And then here we simply return one times two, which is two.
35
00:02:47,270 --> 00:02:51,290
So that's then placed in the array and we move on to the next one.
36
00:02:52,070 --> 00:02:53,420
So then here we have two.
37
00:02:53,600 --> 00:02:56,840
So therefore element becomes two, two times two is four.
38
00:02:56,840 --> 00:02:59,180
And so that then gets placed in the array.
39
00:02:59,360 --> 00:03:02,930
And like this, it goes all the way until the end of the array.
40
00:03:02,960 --> 00:03:09,980
And so again, the map method is basically to create a new array based on the original array with some
41
00:03:10,010 --> 00:03:14,000
operation applied to each element of the original array.
42
00:03:14,850 --> 00:03:17,100
So this was just some demo here.
43
00:03:17,100 --> 00:03:23,730
But now what I want to do is to create an array that contains simply all the titles of all the books.
44
00:03:23,730 --> 00:03:30,360
So instead of having this huge array which contains all these objects, all I want is an array which
45
00:03:30,360 --> 00:03:32,760
simply has all the titles.
46
00:03:36,080 --> 00:03:41,540
So let's say titles equals books, dot map.
47
00:03:41,900 --> 00:03:44,540
And now again, our callback function.
48
00:03:44,690 --> 00:03:53,990
And here, let's call each element a book because, well, we have this books array, which is.
49
00:03:55,430 --> 00:03:55,730
Well.
50
00:03:55,940 --> 00:03:57,380
Well, actually, it isn't.
51
00:03:57,890 --> 00:03:58,970
Isn't anything.
52
00:03:59,240 --> 00:04:01,010
And that's because we have this error here.
53
00:04:01,430 --> 00:04:05,090
Let's just return something here so that the error ends.
54
00:04:05,840 --> 00:04:06,200
Now.
55
00:04:06,320 --> 00:04:10,100
But yeah, this is not the books array anyway.
56
00:04:12,150 --> 00:04:12,360
Yeah.
57
00:04:12,360 --> 00:04:15,240
So here we have this array with all these objects.
58
00:04:15,240 --> 00:04:20,820
And so now as we loop over this array, each of the elements is one book object.
59
00:04:21,500 --> 00:04:22,250
Right.
60
00:04:23,560 --> 00:04:25,510
So this is the argument to the function.
61
00:04:25,510 --> 00:04:27,850
And then remember is what we return.
62
00:04:28,060 --> 00:04:30,280
So what do we want to return?
63
00:04:31,160 --> 00:04:33,950
Well, simply book that title.
64
00:04:34,660 --> 00:04:40,870
And now as we check out what titles is we see, it is this array which only contains the titles.
65
00:04:41,630 --> 00:04:42,460
Beautiful.
66
00:04:42,470 --> 00:04:45,170
So that was pretty simple, right?
67
00:04:45,170 --> 00:04:50,660
So the operation doesn't, of course, have to be like a math operation like this here.
68
00:04:50,660 --> 00:04:54,380
Simply what we return for each book element is book title.
69
00:04:54,380 --> 00:04:58,190
And so that then gets placed into the array.
70
00:04:58,190 --> 00:05:02,450
So the new output array, which then gets stored into this titles.
71
00:05:02,990 --> 00:05:10,190
Okay, let's see another example and let's say that we wanted to basically only get the essential data
72
00:05:10,190 --> 00:05:12,860
for each of these book objects.
73
00:05:13,920 --> 00:05:16,650
So we want an array with objects still.
74
00:05:16,650 --> 00:05:20,300
But now we only want, like, the title and the author.
75
00:05:20,310 --> 00:05:20,910
Maybe.
76
00:05:21,800 --> 00:05:24,170
So let's say essential.
77
00:05:25,570 --> 00:05:28,570
Data should be books.
78
00:05:28,570 --> 00:05:29,950
Dot map.
79
00:05:31,000 --> 00:05:33,340
And again, I'm calling each one the book.
80
00:05:33,670 --> 00:05:39,940
And now here what we want to return is a new object which contains, again, the title and the author.
81
00:05:40,620 --> 00:05:46,200
Now in an arrow function, whenever we use these curly braces like this, the arrow function will think
82
00:05:46,200 --> 00:05:48,480
that this is a declaration block.
83
00:05:48,790 --> 00:05:49,440
Right.
84
00:05:49,440 --> 00:05:51,720
So this is not returning an object.
85
00:05:51,720 --> 00:05:59,040
So if we wrote like x 23, this would not be returning anything because again, the arrow function simply
86
00:05:59,040 --> 00:06:02,880
thinks that this here is the function block.
87
00:06:03,210 --> 00:06:05,080
So we can solve this in two ways.
88
00:06:05,100 --> 00:06:08,550
First, we can simply then return the object from here manually.
89
00:06:09,270 --> 00:06:10,590
So we can now say.
90
00:06:11,390 --> 00:06:14,390
Title is booked.
91
00:06:14,420 --> 00:06:15,320
Title.
92
00:06:16,580 --> 00:06:21,050
And author is book dot Author.
93
00:06:21,940 --> 00:06:23,500
Let's take a look at that.
94
00:06:25,070 --> 00:06:25,970
And.
95
00:06:26,910 --> 00:06:28,200
Yeah, that works.
96
00:06:28,200 --> 00:06:32,700
So now for each of the books, we only have the author and the title and nothing else.
97
00:06:33,800 --> 00:06:34,880
So that worked.
98
00:06:34,880 --> 00:06:36,090
So that's great.
99
00:06:36,110 --> 00:06:39,080
But yeah, here we have a bit too much code.
100
00:06:39,110 --> 00:06:41,570
So let's get rid of this return again.
101
00:06:42,710 --> 00:06:46,910
And so the trick is to simply wrap this here in a parentheses.
102
00:06:48,990 --> 00:06:50,640
So we don't need any of this.
103
00:06:52,000 --> 00:06:53,620
And so now that works the same.
104
00:06:53,620 --> 00:07:00,400
And so by using these parentheses first instead of the curly braces, then again, we are automatically
105
00:07:00,400 --> 00:07:02,890
returning this object from here.
106
00:07:03,640 --> 00:07:06,700
So just like here we are returning the result of this operation.
107
00:07:06,700 --> 00:07:10,420
And here we are returning simply this value right here.
108
00:07:11,160 --> 00:07:14,250
And of course, we could also do some computations in here.
109
00:07:14,250 --> 00:07:22,440
For example, let's say we wanted to reviews count and now here we could get back this function that
110
00:07:22,440 --> 00:07:23,580
we did before.
111
00:07:23,730 --> 00:07:25,020
Let's just copy that.
112
00:07:29,210 --> 00:07:35,360
And then let's just call get total review count for the current book.
113
00:07:37,350 --> 00:07:43,290
And so now, yeah, we get all of this data that we wanted, but again, it's just the essential data,
114
00:07:43,290 --> 00:07:49,680
just basically a small part of the total data that we had before in the books array.
115
00:07:49,770 --> 00:07:56,210
So these are just a few use cases that are going to become in handy for the rest of the React course.
116
00:07:56,220 --> 00:07:59,340
And if this was maybe a bit too fast.
117
00:07:59,340 --> 00:08:06,330
So if you didn't know this map method before and you didn't quite understand it yet, that's understandable
118
00:08:06,360 --> 00:08:10,860
because this was more of a crash course on this map method.
119
00:08:10,860 --> 00:08:18,450
And so if somehow it hasn't clicked for you yet, you can just check out the documentation on mSDN or
120
00:08:18,720 --> 00:08:24,090
of course you can also check out my complete JavaScript course because there I explain it all in a lot
121
00:08:24,090 --> 00:08:24,930
more detail.
122
00:08:24,930 --> 00:08:27,510
But for this short introduction, this should be enough.
123
00:08:27,510 --> 00:08:32,550
And so let's now move on to the next important method, which is the filter method.
11274
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.