After the very important map, filter
and reduce methods,
we still have some more methods to learn
which are also super important and used all the time.
So in this lecture,
we're gonna talk about the Find method.
So as the name says,
we can use the Find method to retrieve one element
of an array based on a condition.
So let's again, use all movements,
and so find and just like the other methods
that we've been talking about,
the Find method also accepts a condition.
And just like the other array methods we've been talking
about the find method also accepts a callback function
which will then be called as the method loops
over the array, all right?
So Find is basically just another method
that loops over the array
but then it does something different.
And in this case
what the Find method does is
to retrieve an element of the array.
So as always the current element of the iteration
is the movement and then here we specify a condition.
So let's say movement less than zero,
so basically this is here, a withdrawal, right?
So a negative movement.
So you see that just like the Filter method,
the Find method also needs a callback function
that returns a Boolean.
So the result of this is of course,
is either true or false.
Now, unlike the Filter method,
the Find method will actually not return a new array
but it will only return the first element
in the array that satisfies this condition.
So basically in other words,
the first element in the array for which
this operation here becomes true.
And so basically this here will return to first withdrawal.
So first withdrawal
is this.
So lets log the movements again to the console
and then let's also log the first withdrawal.
And so indeed -400 here is
the very first value
that appears in the array that is negative,
okay?
So as you see, the Find method is a bit similar
to the Filter method,
but there are two fundamental differences.
First Filter returns all the elements
that match the condition while the Find method
only returns the first one and second
and even more important, the Filter method returns
a new array while Find only returns the element itself
and not an array, okay?
So make sure that you understand
this fundamental difference.
Now this example here is not really exciting,
and so let's now take it to the next level
and start actually working with our array of objects.
So that's this accounts, array, remember,
so this array, which contains the four objects where each
of them is one account.
And as I mentioned earlier,
this is a pretty common data structure in JavaScript,
and so let's now work with this together
with the Find method,
because in this context,
the Find method actually becomes really useful.
So that's because using Find,
we cannot basically find an object
in the array based on some property of that object,
and so that's really cool.
So let me show it to you.
So let's create a variable called Account
and it's gonna be accounts.
So all the accounts.find and then, or callback,
and so as we loop over accounts
each of the current elements is one account,
okay?
And so now let's say we want to select one
of the accounts by the name.
Now, this disappeared here so let's bring it back,
okay?
So let's say we want to get the account
where the owner is Jessica Davis, right?
So we can simply do account.owner=Jessica Davis.
So let's see,
just the account,
and indeed we get now only this object
and this is really, really powerful of a race and objects.
So where one array contains multiple objects
which all have a similar structure.
So all of them here have an owner
have a movement, a pin, a username, and so on and so forth.
So using the Find method, we can
then search this array basically
to find an object that matches
a certain property that we already know.
So in this case, that property that we know
is the name, all right?
And so we simply compare Jessica Davis here
with the account owner.
And whenever this condition here is true,
well, then that object is returned.
So usually the goal of the Find method
is to just find exactly one element,
and therefore we usually set up a condition
where only one element can satisfy that condition.
And so that's why we used the equal operator here, okay?
So if the owner names are unique
then this equal operator here
will only ever match one account with this name here.
All right, and we will actually use this in the next couple
of lectures to implement the login feature,
and also some other features.
Now, I'm gonna leave it to you here
as a challenge to implement this functionality using
the for off loop, just like we have been doing it
with some of the other methods, like map and filter.
So we have also been implementing
these using the for off loop just to see the difference.
But now I'm not gonna do that
but I'm leaving it here as an idea for you to do that.
Now anyway, let's go now to the next video
and actually start to implement the login functionality.