JavaScript ES5 some() Method

JavaScript
Transcript

English (Auto-generated)

in this podcast, we'll talk about the some method. So the sum method will determine whether a queried value exists in an array and it does this by executing a callback function for every element in an array. Now, if the queried element or value is found, then the bullying true is returned else. The boolean false is returned. And let's try this out by taking uh taking a look at this example first. So hackathon team is an array of a few objects and each object has a name, property and a rural property. And there are four objects in the Hackathon Team Array. And we are going to use the summer method on the hackathon team array and we want to query this array to see if there is a designer role. So we might have a bunch of teams and the teams only qualified if each team has at least one designer role. Right? So instead of looping through every item of the array and querying each key value pair in an object. We'll use the some method. Now the some method doesn't mutate the original array, so we will declare a new variable and we'll call it designer. So that designer is equal to the results of the sum array method on the array. Hackathon team. The hackathon team and some like so and keep in mind that some takes as an argument, a callback function. So let's structure the callback function within the place where we put our arguments for the method like this and now we want to return true if the item, so item out here and the callback function takes item as an argument. So item refers to each item, object item in the array and we want to return true if item dot role for any of the rules is strictly equal to designer. So remember the condition is that each team must have at least one designer to qualify for participation. Right? And then what we can do is now we could do a console dot log to see the results. But before I move on, let's just recap. So some method is called on hackathon team array. And the method calls back a function which takes as an argument item and item refers to each array item here, which is each object and its property value pair. And within this body of the callback function, we are going to query the value of the role key in each item to check if it's strictly equal to the string designer and then this true or false result is going to be assigned to the variable. Designer. Right. And we could even say designer exists or name this variable anything. Let's just let it be designer. Now, I will just structure some if else statement here so that we can check the result on the page. So if designer Right? So if designer, if the value of the designer, variable is true, then I'm going to insert something into the body of the document. So document dot body. Inner html is equal to a strength. Right? And the string is Yeah, the team has a designer else. We will just And why don't I use addition um assignment. So we're appending uh to the dom else a document dot body dot inner html. Tradition assignment. Ah No designer found in the team. Right. Um so that looks fine. Um That's fine. We could have just we don't need to use the template strings so we can just maybe use our courts. I mean we're not inserting any variables here so we can just use our coats. No. Let us control and as a safe. Okay, let's see what's going on here. And this is our html. And so if item dot role is strictly equal to designer, we could even just check it out with um with this alert. Okay. I guess I have to close this and then in my packages open preview html again. But let's open it for html because that's where we want to see a result. There you go. So now we've found that yes, this team, Hackathon team has a designer in it. And if I just change this to something else, for example, I could just do develops and then control? S there you go. No designer found in the team. So e. S. five. Some method is a pretty handy and it's a little faster than iterating through an array and it's just going to return true if the query the value exists in your hurry
166 Views 0 Likes 0 Comments

Let's use the ES5 some() method to check whether a queried item exists in a JavaScript array

Comment
Leave a comment (supports markdown format)