JavaScript ES5 every() Method

JavaScript
Transcript

English (Auto-generated)

Hi everyone in this podcast, we'll talk about the every method. Now, the every method is going to execute a function for every element in an array and this method checks if every element in an array passes some kind of test or condition if so, then the bullying true is returned else. If any element in the array does not pass the test, then the bullion false is retrained. So let's take a look at our array of objects. And this array is called hackathon team and it has four objects each with a property value pair. And in this array uh each object has about three property value pairs. Right? And name, role and enrolled. So we're going to now use the every method and we want to check. So the condition is that every team member must be enrolled in the hackathon to take part. So if any of the team members is not enrolled then the team is disqualified. They can't take part unless the team member enrolls. Right? So the enrolled property for each member must have a value of true. And to check if it does, we're going to use the every method which executes for which executes a callback function for each array item. To see if it passes a test. Now, since this method does not mutate the original array, we will declare a new variable and assign the result of this method to this variable and we'll call the variable enrolled. So let enroll is equal to hackathon team. And we are going to apply that every method on the hackathon team, like. So now this method takes as an argument, a call back function and we'll pass item as an argument to the callback function and we want the callback function to return true or false by checking if each objects enrolled property is strictly equal to the bullion true. Okay. So let's see what's happening here. We are using every method on the hackathon team array and this every method is taking takes as an argument, a callback function which executes for each object item in the array and it takes item as an argument. Right? So the function is checking if the enrolled property of each object item in the array has a value of the bullying true. If so if every object passes the test then we're going to assign um the value the bullying true to enrolled. If any one of the team members was not enrolled then it would be false. So let's check this with a false first. Right? Or let's just let it be to truth. And what I'm going to do now is I'm actually, I could do alert or console logs but let's just put it in a let's just put this in a um in the dome. Right? So I'll just make an if else a statement and it's much, I think it's much nicer to see it in the dom makes it maybe a bit more exciting. So if enrolled then we want to say well um you know we want to do document dot body dot inner html and plus equal to and we can say something like every team member is enrolled. And else we want to change or appended to the inner html of the document a string letting them know that not all team members are enrolled. Not all team members are enrolled. So for now we can see that. Indeed, yes, every team member is enrolled in our array of objects. So let's control us to save and there you go. So now we know that every team member is enrolled and we get that on the web page. Now, if we change this to false. So let's change the designer to not be enrolled. So false control. S and we are now not seeing anything. And I wonder if I have to kind of reload this again. Mhm, mm hmm. Uh let's see what's going on if enrolled. This is true. Okay, enrolled is now false. Let me just try it again. True. Okay, every team member is enrolled. Let's change one to be the boolean false. And for some reason this one is oh, I see why it's not working typo and there you go. So we had a small typo here, which is why uh, you know, this was not being the string was not being appended to the dom but we see that not all team members are enrolled now. So this is a pretty handy method which is basically executing on each item or object of our array, and basically it's going to return true if all of the items are passing a condition or the test, a test, which for us was that enrolled property must be true in order for the team to participate in the hackathon.
243 Views 0 Likes 1 Comments

This is a handy method which will return the first occurrence of a value in an JavaScript array

Comment
Leave a comment (supports markdown format)
@jesusvilla 2 years ago

JS array methods are amazing