JavaScript Every & Some Array Methods Tutorial - Google Sheets Apps Scripts - Array Methods Part 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so let's talk about our next to array methods one of them is gonna be every and the other one is gonna be some so let's get our app script editor started there we are I'm gonna create an array so as always all started with a very basic array and go from there so here we go so now I'm gonna take that array and I'm going to use a method on that call every and every method will take a function so I'll have the function here right inside of this and this function is gonna pass an argument we'll call it num now let's create a variable results and then we'll lock those results right here okay so here we are let's see how this works so every method is built to check if every single element in your array is passing a condition so let's see an example of that I'm gonna say return num greater than 6 so I'm checking if every single element in this array will pass this condition and my condition is is my element greater than 6 now this one is not so most of them are not with exception of 7 right we should get a false so let's see what we get I'm gonna run this ok let's see what we get so we got false so let's check if it's greater than 3 again see not every element in this array is greater than 3 a lot of them are like these 3 are but not all of them so if we're on this look at this it's still false now if I check if everything is less than nine now see every single element is less than nine so if we run we return true because every single one is actually less than nine so the idea of this is this we're giving it returned results that's either true or its false right so you can create an if statement instead of doing this so you could say if num is less than nine then let's return true else let's return false so if I run this still the same thing right so it's just not necessary to do this it's a lot easier to just to return this because that's gonna return either true or false by itself so getting back to this so this is what's gonna happen we get this one so we're gonna first one here so just to illustrate our results so first one is less than nine so we get true then the second one which is two is also less than nine so we get true the third one is less than nine we get true so we keep getting all this truth so in the end whatever is doing if every single one of these is true then the whole thing is true if at least one of these ends up being false one or more now this whole thing is gonna be false and that's the results we're getting with every now similar to every there is also some so some is similar in the way it works to every the only difference is now we're checking if at least one element is passing the condition so right now if I run this is number less than nine every single one is less than nine so this should be true true see so if I say is number less than three I have at least two that are less than three so this should also return true and if I simply just say is it equal to four well I have at least one number that's four so this will pass that condition so if at least this one is passing this then the whole thing should be true again still true see and finally let's get a case of false right I'm gonna say eight now there's not a single number that's passing this condition so this should be false in a way every is and condition and sum is or condition with every you have and condition which means every single one of those arguments that you're passing needs to be true and or means at least one of them needs to be true and the whole thing will pass now let's do this as usual with a two-dimensional array now I have this two-dimensional array if I do every well now this number is no longer a number it's this array every time we're passing one of these a race so I'm just gonna call this our four row thinking this is one of our rows doesn't have to be but usually with Google sheets we'll think about this as a row and now in that row I can say is row 0 0 is what this one the first element I'm gonna say is that equal to B so if I run this it should say false there isn't it's false is because every that means every single one of these should have been B so the only time this should have been true is when our array is like this C true because every single one of them is B so we get true for every single one of those logical statements now let's use the numbers so number is the second index in this array and we'll say if that number is greater than 5 now every single one of these is greater than 5 so this should get us true right yep and if I do is it greater than 7 well one of them is greater than 7 but the rest aren't so false right and the same way if I do some well at least one of them is greater than 7 so this should be true and it is and that's our every in some now let's try to use it on this spreadsheet and this piece of data so if you look through this data basically it's not very organized data so we have a lot of blank rows in the middle of this data let's try to clean it up so what we're going to do we're going to try to first delete every row that's blank out of this data set if you look see we have some blank cells in the middle I don't want to delete those I only want to delete the ones where the whole row is blank okay so let's get to our script I'm gonna get rid of all this stuff now I'm gonna first access my spreadsheet then we'll create our active sheet and then in this active sheet we'll get our data range which is starting from second row and all the way down to an array VAR o data for original data we're gonna go to our active sheet get range start from the second row column 1 and then how many rows going from the bottom 204 so 204 rows - the label on top that's 203 so I can do this but instead I'm gonna get this 203 dynamically by simply taking our active sheet and getting my last row -1 so last row is gonna give us that same number we used to get there which was this 204 and then minus 1 should get us to 2 or 3 so that's that and finally the number of columns so 1 2 3 4 5 6 7 and we'll get values that should create an array of our data for us now let's just quickly take a look at this and see what it looks like always a good idea to kind of step through to see what's going on so if you look see this is our data now just to refresh your memory we had some blank rows right so over here the first one is this one right after this Columbus Ohio so if we look here see there's that Columbus Ohio and if you look there's this array of blanks right here so that's one of the race we need to get rid of now we need to basically filter out every row that's completely blank so I need to use filter function because I'm filtering this right so I'm gonna take my old data dot filter if you don't know about filter function then you should watch my filter video now filter function will take a callback function and callback will be a row we're gonna pass a row to this function it's gonna basically go through different rows and every time but it turned true it's gonna keep the row every time we return false it's gonna remove the row what we're going to do since this row is gonna be this entire row which is in our log it's an array right so for example the first time it's gonna be this array the first one so in this array we need to check if everything is blank and the way to do it is by using every write because every will check if every single argument is passing the condition I'm gonna do every and here we're going to do another callback function and in this callback function we'll just pass in a row just to have some separation of variable names in a row and we'll say equals double cult so we're gonna return that which means this is gonna check if the row argument is blank actually let's just call this cell because it's not exactly an intro because we were checking every single cell so we're passing a row and in the cell we're gonna check if the cell is blank and if all of them are blank this every is gonna return true so if we simply return this which is going to give us true when every single one is blank then it's gonna keep the blank ones only so this is not what we're looking for but let's see the results of this so if I log this oh you know what we need to lock the data see what we're getting is all the blank rows because what's going to happen this is gonna return true every time every single one of them is blank right so what we're going to do we need to just flip our results right because what we're getting right now is just the ones that are blanks we need the opposite so to do the opposite I'm gonna take this this returns our true or false based on this every I'm gonna do an exclamation sign and that will basically just switch trues two falses and false it's a truce so let's see what we get I'm gonna run this log see all the blank rows in the middle now they're gone so now that we down this we need to take our new data and remove the old one and place the new data here without all the blanks to do this I first have to clean this up so I'm gonna go to my active sheet I'm gonna do get range and we're gonna start and to make this simpler I'm just gonna use regular string syntax so that's from a two through G all the way down so I'm gonna do a two through G and then I'm going to simply just clear content that will clean it up now after it's clean we'll take our active sheet to get range we need to place our new data where it's supposed to go we're gonna start placing this in here in this cell that second row first column so two one and then we'll calculate the rest in this data dynamically data dot length comma data zero dot length I've done this many times before if you don't understand this watch my previous videos I cover this so I'm gonna do set values and we're gonna set values to this data range semicolon I don't need to log this out anymore save I'm gonna run this go take a look see all the blank rows are now gone nice you can see how the blank cells are staying here so we're not removing blank cells we're removing blank rows I'm gonna undo this now what if I wanted to remove every single row where there is a blank I don't want to remove where the whole thing is blank I want to remove where at least one cell is blank so that means I need to remove this one I also need to remove this one also this one also this one and also this one right so to do this what we'll have to do instead of using every we're gonna use some right so if I change this to some what's going to happen this is going to give me true if at least one of them is blank and then if we take that and reverse that for our filter that should just work so that's the only thing I should need to change here let's look at our data this is what it looks like I go back and run go back take a look see every single row where there was a blank cell it's gone and that's our every and some methods that should do it for this video thanks for watching please subscribe and I'll see you in the next one
Info
Channel: Learn Google Spreadsheets
Views: 10,359
Rating: undefined out of 5
Keywords: JavaScript, Every, Some, Array, Method, Google Sheets, Apps Scripts, how to, Tutorial
Id: gaC290XzPX4
Channel Id: undefined
Length: 17min 59sec (1079 seconds)
Published: Wed Nov 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.