Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
After the very important map, filter 2
2
and reduce methods, 3
3
we still have some more methods to learn 4
4
which are also super important and used all the time. 5
5
So in this lecture, 6
6
we're gonna talk about the Find method. 7
7
So as the name says, 8
8
we can use the Find method to retrieve one element 9
9
of an array based on a condition. 10
10
So let's again, use all movements, 11
11
and so find and just like the other methods 12
12
that we've been talking about, 13
13
the Find method also accepts a condition. 14
14
And just like the other array methods we've been talking 15
15
about the find method also accepts a callback function 16
16
which will then be called as the method loops 17
17
over the array, all right? 18
18
So Find is basically just another method 19
19
that loops over the array 20
20
but then it does something different. 21
21
And in this case 22
22
what the Find method does is 23
23
to retrieve an element of the array. 24
24
So as always the current element of the iteration 25
25
is the movement and then here we specify a condition. 26
26
So let's say movement less than zero, 27
27
so basically this is here, a withdrawal, right? 28
28
So a negative movement. 29
29
So you see that just like the Filter method, 30
30
the Find method also needs a callback function 31
31
that returns a Boolean. 32
32
So the result of this is of course, 33
33
is either true or false. 34
34
Now, unlike the Filter method, 35
35
the Find method will actually not return a new array 36
36
but it will only return the first element 37
37
in the array that satisfies this condition. 38
38
So basically in other words, 39
39
the first element in the array for which 40
40
this operation here becomes true. 41
41
And so basically this here will return to first withdrawal. 42
42
So first withdrawal 43
43
is this. 44
44
So lets log the movements again to the console 45
45
and then let's also log the first withdrawal. 46
46
And so indeed -400 here is 47
47
the very first value 48
48
that appears in the array that is negative, 49
49
okay? 50
50
So as you see, the Find method is a bit similar 51
51
to the Filter method, 52
52
but there are two fundamental differences. 53
53
First Filter returns all the elements 54
54
that match the condition while the Find method 55
55
only returns the first one and second 56
56
and even more important, the Filter method returns 57
57
a new array while Find only returns the element itself 58
58
and not an array, okay? 59
59
So make sure that you understand 60
60
this fundamental difference. 61
61
Now this example here is not really exciting, 62
62
and so let's now take it to the next level 63
63
and start actually working with our array of objects. 64
64
So that's this accounts, array, remember, 65
65
so this array, which contains the four objects where each 66
66
of them is one account. 67
67
And as I mentioned earlier, 68
68
this is a pretty common data structure in JavaScript, 69
69
and so let's now work with this together 70
70
with the Find method, 71
71
because in this context, 72
72
the Find method actually becomes really useful. 73
73
So that's because using Find, 74
74
we cannot basically find an object 75
75
in the array based on some property of that object, 76
76
and so that's really cool. 77
77
So let me show it to you. 78
78
So let's create a variable called Account 79
79
and it's gonna be accounts. 80
80
So all the accounts.find and then, or callback, 81
81
and so as we loop over accounts 82
82
each of the current elements is one account, 83
83
okay? 84
84
And so now let's say we want to select one 85
85
of the accounts by the name. 86
86
Now, this disappeared here so let's bring it back, 87
87
okay? 88
88
So let's say we want to get the account 89
89
where the owner is Jessica Davis, right? 90
90
So we can simply do account.owner=Jessica Davis. 91
91
So let's see, 92
92
just the account, 93
93
and indeed we get now only this object 94
94
and this is really, really powerful of a race and objects. 95
95
So where one array contains multiple objects 96
96
which all have a similar structure. 97
97
So all of them here have an owner 98
98
have a movement, a pin, a username, and so on and so forth. 99
99
So using the Find method, we can 100
100
then search this array basically 101
101
to find an object that matches 102
102
a certain property that we already know. 103
103
So in this case, that property that we know 104
104
is the name, all right? 105
105
And so we simply compare Jessica Davis here 106
106
with the account owner. 107
107
And whenever this condition here is true, 108
108
well, then that object is returned. 109
109
So usually the goal of the Find method 110
110
is to just find exactly one element, 111
111
and therefore we usually set up a condition 112
112
where only one element can satisfy that condition. 113
113
And so that's why we used the equal operator here, okay? 114
114
So if the owner names are unique 115
115
then this equal operator here 116
116
will only ever match one account with this name here. 117
117
All right, and we will actually use this in the next couple 118
118
of lectures to implement the login feature, 119
119
and also some other features. 120
120
Now, I'm gonna leave it to you here 121
121
as a challenge to implement this functionality using 122
122
the for off loop, just like we have been doing it 123
123
with some of the other methods, like map and filter. 124
124
So we have also been implementing 125
125
these using the for off loop just to see the difference. 126
126
But now I'm not gonna do that 127
127
but I'm leaving it here as an idea for you to do that. 128
128
Now anyway, let's go now to the next video 129
129
and actually start to implement the login functionality.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.