JavaScript Array Every Method Practice in 5 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you want to get better at javascript well arrays are an essential part of javascript so in this video let's look at the array every method in about five minutes so first off if you're new to the channel welcome i do weekly videos on web development topics so if you're interested in more of that content make sure to subscribe so you can see it when it comes out all of that said let's dive into arrays alright so this is a series on javascript arrays and i've got a github repository here that inside this github repository has a readme on what we'll work on and then a worksheet that you will check out to be able to use below so inside of that worksheet you'll have some sample star wars character data that we're going to work with and we're going to walk through in these different videos mapreduce filter sort every sum and then maybe we'll add some no pun intended extra videos to this on the end if you're interested so let's go ahead and dive in all right so i've got the array functions practice repository checked out with the worksheet and inside of this worksheet it has a sample characters array with some star wars data so an array of characters each one has a name height mass eye color and gender property so those are the pieces of data that we're going to work with and we're going to scroll all the way down to the every section and what every we'll do is we can give it some sort of condition and it will check to see if every item in that array meets that condition so instead of having to do that stuff ourselves we can just tell we can use every and then tell javascript how to know how to know what the condition is that we're trying to look for so uh let's do this let's create a variable all blue eyes and we'll take our array so characters and then we'll call dot and then we'll call the every function and every will take a callback function that gets a reference to each character each item in the array and then what we want to do is determine what the condition is that should be met so what we want to do is return whether or not the character dot i color equals and let's just see how this is spelled up here so just lowercase blue so we want to see if the eye color equals the string blue so then let's log out the all blue eyes and let's see what we get so uh for running this i'm gonna use a nodemon which i've got installed globally on my machine and then give it the javascript file that i want to run and this says false which uh should is correct i was going to say is true but is correct so false is correct because there are characters with brown eyes and not blue eyes so that is right and uh so this every again is checking based on this condition if each item in that array meets that condition now one of the cool things we can do with this being a callback function that is an es6 arrow function since it's only a one line return we can actually get rid of the return keyword get rid of the semicolon and the brackets for the function and then this will kind of format this for me but this is basically a one-liner here to determine whether or not all the characters have blue eyes all right so let's do one that's a little bit similar here let's check to see if every character has a mass of more than 40. so i'll say all mass more than 40. uh whatever a little bit long variable name but that's okay so we'll take our character's array we will call we will call the every function we'll pass it our callback function so uh get a reference to each character in here and then what we want to do is return whether or not the character dot mass is greater than 40. uh and then we can log this out so let's do a console log of all mass more than 40. uh let me know if you have a better variable name for that in the comments below let's log that out and this one logs out as true so we have false for the first one and then true and if we look at our data all of these masses are greater than 40. 49 for leia is the lowest and it is greater than 40 which is pretty cool so in another one liner we're able to check whether or not all of them uh have a mask greater than 40. so then we want to check uh all shorter than 200 and i don't actually know what the metric is it's just the number 200 so we'll just run with that but we'll take our characters we'll do every call to every function pass in this arrow function that takes in the character as a parameter and then what's the condition that we're checking for well we're checking to see if the character dot height is greater than 200 so with that in place we can log out the all shorter than 200 are all uh shorter than so it should actually be less so less than 200 if we say this that becomes false and i think that's because if we look at darth vader up here darth vader has 202 for a height so that makes sense and then the last one in here is every character male so let's see all male and i'll do characters dot every we'll pass in our function in here that takes character as a parameter then we'll return whether or not character dot gender triple equals compare the string of male and this one should return false because we have leia in here who is a female so let's log out the all male here and we should have false true false false so the every function is pretty neat here of we can give it some sort of condition to check so this is a callback function that we pass into every we get a reference to each character or you can name this item or whatever you want it to it's an arbitrary name and then we tell it what the condition is that it should meet and then it will do the rest to tell us if each one of the items in the array matches that condition all right i hope you enjoyed that video and you feel like you're getting better at javascript arrays if you're interested in learning more about arrays make sure to check out one of the other videos in the playlist and if you have any additional questions or topics in javascript you'd like to see covered feel free to leave a comment below or check out the link to join the learn build teach discord server and you can ask and share there as well thanks again for watching the video and i'll catch you in the next one
Info
Channel: James Q Quick
Views: 16,531
Rating: undefined out of 5
Keywords: javascript array every, array every, javascript every function, javascript arrays, javascript array functions, javascript array methods, web development, vanilla js, learn javascript, learn javascript arrays, become a web developer, js arrays in 5 minutes, arrays explained, javascript tutorial for beginners, web development tutorial for beginners, javascript tutorial, web development 2021, array map explained, javascript array practice, web developer, javascript programming
Id: Arbk085MWjc
Channel Id: undefined
Length: 6min 12sec (372 seconds)
Published: Thu Jan 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.