Javascript Array Some

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everyone today we'll be looking at the javascript array method called sum and what this method does is it checks through an array so it basically loops through the array and if a given predicate function passes the test even one just one time the whole thing will return true and if it never returns true it will um the whole thing will return false so that's the bigger picture of it so let's um get straight into so we'll have an array of objects a sample data set here certain array of objects called avengers so there's six avengers inside which is an object and three properties can fly strength and name and so we have three challenges here and we're gonna see exactly what the sum method does so challenge number one check if any of the avengers name has the word black in it so basically for all these um challenges they're all going to return either true or false so that's what we need to do so the first one check if any avenger has the name black in it so all of them have a name property and we just need to find out that um does any of the names have the word blackness so just before we do it we're gonna we're gonna see that it's gonna be true because this name here has black in it black and this name also has black in it so the whole thing should we should expect an answer true so let's get straight into it so the way we do it is to say avengers dot sum just like this and what this is a it's an array method so we do that and then it takes in a function and this function is very important it's called a predicate function and this function is going to run for every single element inside the array you have to return either true and true or false so we're either going to return true or we're going to return false for every single um element inside this array and so for each element it's going to get an avenger property like that so we're going to have access to the avenger property and we're going to run this function every single time so basically what we want to do is just say just return avenger dot so name dot includes black like that so yeah just black because it's capital here so yeah so return um avenger dot include avenger.name this will get replaced by say um it's a black widow like that and we're going to say so dot includes is a method on strings and so if it includes the word black so this does so this will return true and so yeah that's the way that works so um the way some works is that if it returns true even once this entire thing will return um true but um if it runs through the entire array and it never returns true the whole thing here will return false so we have to store this into a new variable so i'm going to call this result so it's going to be a boolean value right here so this is going to be a type either true or false and we can just go ahead and log that so result and let's see what this does so it returns true why because it ran through the entire array right it ran this function and so it has to return this expression here has to return true at least one time not for all of them but one time and it does that so um when it gets to here it says false okay false and then here it returns true because this one will pass the test and it returns true again but it doesn't have to return true twice so it just at least one time the whole expression here will return true and that's exactly what we have here so let's just say we had something different so let's just say it was false it always returned false no matter what so the whole expression returns false right because it would fail to test so sum just means like you can also think of it as like any so it has to be any any item here returns true this entire expression will return true so similar to the array method called every but whatever he does is everything has to pass the test so everything has to return true but for some it only has to return true just one time and that's it and then the entire expression will be true and if it doesn't return even once the entire expression will be false so the result will be false so that's challenge number one so let's just go ahead and yeah so avenger.name.include black and that will pass the test at least one time so that's why the entire expression here is true okay so moving on to challenge number two so let me just go ahead and erase this so challenge number two says check if any avenger has a strength index of more than a hundred and ten so if you notice the array they all have a strength property like this so all we have to do is check if any of them has um a strength index of more than 100 so if any avengers at least one has to have a strength next of more than 110. so let's get straight to it so i'm going to just start by saying the result is going to be equal to avenger.sum and remember it's gonna it takes a predicate function right so function like that and we're gonna for each function it's gonna run uh one single avenger so avenger like that it has to return either true or false right so what do we want to check here so check if any avenger has a strength index of more than 110 okay so the test will be avenger so we want to get access to that strength it property right so i want to see if this is greater than 110 so more than 110 right so it's going to run this for every single time and if it passes this test at least one time this entire thing will return true and we'll just go ahead and log that result so notice it's false why is it false because um it's going to check this test so is the strength here more than 100 nope false false false false false false it never returned through even one time so that's why this entire thing is false so let's say maybe if it was just 10 right so just 10 yeah 10 it returns true why because yeah all of them i guess have it that are greater than 10. so let's just say 90 right so 90 returns true y because even if this fails and this fails and this fails we still have at least one right that returns true so this will return true and this will return true so let's just say 99 yes all of them would fail that test except this one here right so we only need at least one so that's why i returned true let's say we had 101 right so false because none of them so of course the challenge was 110 right so that's why this whole entire expression here is false and returns this so the result would equal to false and that's why it's printing false just like that okay so let me just um show you how to reduce this down to even just one line of code using es6 i'm going to go ahead and contact um comment that out and so i'm going to say const const result is equal to avengers.sum and i'm going to use an arrow function so each so avenger like that and it has to return so dot avengers. is greater than 110 just like that and of course we'll get the same exact thing right so this is the exact equivalent here so but we don't have to use the function keyword the return keyword and we have to go to a new line we can just keep it all on one line so this function here so avenger will be the property and then or the parameter right and then it's going to simply return either true or false and this is the expression that returns true or false like that so that's basically challenge number two let's move on to challenge number three so let me just make some room here okay so challenge number three so check if any of the avengers can fly so let's say they're on they have to go on some kind of mission and they need at least one person who can fly right so we can use that array method sum to check that so const result is equal to avengers dot sum so i'm going to go ahead and use the arrow function so i'm going to take in the avenger as the parameter and what is it going to return so check if any of the avengers can flash all we need to do is return avenger that can fly just like that because this is by itself is a boolean value so we're going to return that so let's see what this what this does result so it's gonna return true why because it's gonna um does any of these avengers pass this test yeah so this one can fly and then this one can fly right so all we will need uh of course we only need one right but we have two so notice um unlike every every for if we if this was every uh it would be a false y because not all of them so for every all of them have to pass true but the difference here some only well just one has to pass and notice this we have two here so let's say we didn't even have this one here right it would still return true why because we have at least one but say that we commented that out so it'll be false because none of them passed this test but let me just put that back in so challenge number three is you know successful so if any of the vendors can fly is true right and then if you say you have you want to continue on you can say result you can say at least one can fly right and then else none can fly so if you wanted to continue on using this variable so this is how you would do that but for the challenge number three we only need this so that's that should be good so far so let me log the result okay so that was all three challenges using the uh sum method so just remember sum it runs for our and he needs a function that runs for every single element and this function needs to return to true or false so if this function passes at least one time so if this expression ever returns true this entire thing will return true so all it needs to do is return to at least one time but if it never returns true this entire expression will stay false and then remember i have to store into a new variable so just remember to do that okay so that was the javascript array sum method i hope that explains everything like and subscribe [Music] you
Info
Channel: Alpha Javascript
Views: 163
Rating: undefined out of 5
Keywords: javascript array some, some js, js some, array some, some method in javascript, ecmascript, js array some, javascript some method, some javascript, es6 tutorial, javascript array methods, javascript array functions, javascript array, es6 array functions
Id: BSX64e2XhOs
Channel Id: undefined
Length: 10min 9sec (609 seconds)
Published: Wed Sep 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.