Using includes for membership1:18
So if this array includes 8, and we get true. Or once again, 25, we'll get false. So this isn't part of ES2015. It would be part of ES2017 or ES2016. I can't keep up. But yeah, it's not part of the official ES2015 spec, even though tons of browsers already support it. So just keep that in mind. Anyways, back to the finds method. So when we need to use a condition, well, we could do it like this.
Finding first match1:39
Anyways, back to the finds method. So when we need to use a condition, well, we could do it like this. finds. And we'll use the long form syntax for now. OK. So yeah, think of this almost like .map or .filter. That's going to have the same basic shape. Find me the first item in this array that meets the condition that I set here. So almost like findFirst. Think of it like that.
So almost like findFirst. Think of it like that. OK. So maybe it's return $item = 8. Now once again, this is a good use for indexOf or includes. But just as a first step, yeah, it's going to return the value that meets this condition. OK. Well, let's make it a little more dynamic. Maybe the item is greater than 5. OK.
Maybe the item is greater than 5. OK. Well, that should return 6 because 6 is the first item in the array that meets that condition. Or maybe we want the first odd number. So we could say item mod 2 is greater than 0. And now we get 11. And that's the modulus operator. You can research that on your own if you'd like to learn more. And of course, in this case, you could always cast it to a Boolean, do whatever you want. Now what's nice, of course, is we can still use the arrow syntax.
Finding index and objects3:08
Well, in that case, you use findIndex, and we'll get 4, 0, 1, 2, 3, 4. So imagine something like this. You have some kind of User class, and the constructor will accept a name and whether you are an administrator or something like this. So we will assign it. And then let's set up an array of users. So we'll say new User, we'll do myself, and I will not be an admin. We'll do another one for Jane. She is an admin. And then finally, another one for Jack, and he is not an admin.
She is an admin. And then finally, another one for Jack, and he is not an admin. All right, so we have an array of three items where each one is an instance of User. So now we want to say, find me the first User who is an admin. So somebody maybe who is capable of handling this process or this transaction, whatever happens to be for your website. Well, we can do this. Users::find(first, function($user) { return $user->isAdmin(); }); And once again, I will console.log this so that you can see the output. Okay, and sure enough, we get Jane.
Iterating keys and entries4:41
You have array keys. That will give you the keys of the array, the values of the array. You'd also do the entries of an array. So for example, this one would be Jeff Jordan Way. And if we console.log that, it's going to basically give us almost like an associative array. Now, in this case, we're getting an iterator, and in fact, you get to learn about iterators and generators in the very next lesson. So let's say let items, and then you could say for let name of items console.log name.
