JavaScript Arrays & Methods - Part 1 - Google Sheets Apps Script Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so this video will be about JavaScript arrays and very common JavaScript array methods there are a lot of JavaScript array methods and there's really a lot to cover so most likely this is not gonna be the only video there are gonna be multiple videos but we'll see how it goes so this video will be useful for people who want to get more advanced with Apps scripts programming or really anybody who wants to learn how to work with data in JavaScript so what I'm gonna do this is my little spreadsheet I'm gonna go ahead and open my script editor so tools script editor and here's my function so let's start with some common basic things we need to know so first of all what is in a write right so array is like a variable in which you can store multiple values so let's create a variable I'll call this variable names and I'll make it equal to an array and array syntax square brackets and then I will have comma separated list of names so that's the first person that's the second person so this is an array so if I wanted to get the first thing in this array that would be Linda the way I would do that so I can lock this out to our logger.log and I would do names and then set a square brackets and 0 and that 0 is the position of the item in our array we want to extract now Linda is the first item and position starts from 0 so that should get us the first item so if I run this go back and look at our logs there it is that's Linda now if I do 1 1 will be 0 1 as a second item so that should get us Lisa so let's check that out there we go and finally we can keep going right so that's the position if we want to overwrite a value let's say we want to change Lisa to something else we can just say this equals 2 new name and now I changed this Lisa variable here so if I just lock this out you can see it says new name instead as a matter of fact I can log out the whole array like this and if I look at this see we have Linda new name Peter Joe so this logger.log methods that you have in sheets it's kind of a little weird in a way that it presents everything so right now you can see in the array we have this list of comma separated names it doesn't actually wrap them in quotes like you have the array in JavaScript but you kind of have to assume the codes are there now with a race you have a property called length so if I do names length like this and I run this if I do LOC see it gives me four and what that for is it just counts how many things we have here so we have one two three four names so we get a four as the length of that array with a raise we can also use a lot of methods so when I do the name of the variable names I do period it gives me a lot of different methods that are available well in JavaScript in modern JavaScript you will probably have some more in addition to this but these are some very valuable and common ones to start with I'm gonna start with this method called push so push method is a way to add items to our array so push will add a new item to the end of this array so if I push Steve here let's change this to a name show and then just output that names so I'm gonna run this you the log I have Linda I have job right because we replaced that second person I have Peter I have Joe again apparently and Steve is the last person so I had Joe here and I replaced Lisa with Joe too maybe there should be somebody else there this so now we're gonna have Linda and then the second person here is going to be replaced with Anna and then Peter Joe and then Steve there it is now you can obviously push again and now that's gonna add another one to the end and there it is now arrays also have a method called on shift so there this I don't know why I didn't show up but it's there so let's try this so I'm gonna say that and run this now on shift is similar to push the difference is that push is adding something to the end on shift will add that item to the beginning so now see we add that before Linda with on shift the next mention that we'll be talking about is called pop you don't provide any arguments to this pop we'll simply just drop the last item in this array so well as a matter of fact the last item in this line is not gonna be Joe it's gonna be well we're adding Steve then we're gonna add Olivia that's gonna be the last item and then we're gonna pop remove Olivia so we have to end up with Steve so I'm gonna run this let's look at our log and there it is all Levy is gone because we used pop the next one is gonna be shift shift is similar to pop and what it's going to do instead of removing the last item it's gonna remove the first item so over here we're adding Jake so the shift should remove Jake and you can see Jake is now gone even though we're adding him here it's come at this so this remove last element and this remove first element then this one is add first element and push will be add last element so far this is where we are moving on to the next method let's talk about this method cold can cat soken cat will allow you to combine different erase so for example if I have a second array other names and that equals soo these two names what I can do I can do names dot and then can catch and then I can do that other names array concatenated now if we run this what we're gonna see in the log is gonna be just the first array because what happens it Diskin can't see for example pop shift on shift these methods are modifying our array now concat doesn't modify our existing array so it just returns a new one so if i create a new variable and then just lock that out we'll see that what we did we got these and then we're adding Jill and Bob in the end and that's what that does now let's move on to the next one index off very common one index off is a search function so I'm gonna call this variable position so here we provide what we're searching for let's say I'm searching for Peter let's log this run since as to and what it did it basically was searching for Peter in our array now let's see why is Peter number two in this array so first of all let's understand what's gonna happen without right this is our original names array then we're adding Steve to the end then we add Olivia to the end add Jake to the beginning that would remove the last person that was Olivia in the end then we'll remove Jake and then basically we ended up in the beginning with Lynda Lisa Peter that same kind of thing only the second one is actually Anna because we changed it here maybe we should comment this line so we don't have to deal with it but anyways so what's going to happen here we're searching for Peter here so he's gonna look first one it's not Peter second one is that Peter and the third one is Peter and since our position starts with 0 its 0 1 2 so 2 as the position I'm actually gonna just comment out this lines to avoid any confusion and maybe I also will just comment this and place it here so we can see on the screen what's going on and this I have to comment too because those variables don't exist okay so here we go so now we're searching for Peter right so you can search for something and find a position index off has an optional start position let's let's actually do another example of this so let me add Peter one more time so let's say we have two Peters in this let's see what happens with index off so if I search still gives me - because index off finds the first match so 0 1 2 is Peter now what we can do we can provide a second argument after comma in index off and say let's start searching after certain positions so let's say we want to start searching for Peter from this job position and that's 0 1 2 3 so I'm gonna run this and see now we're finding 4 as a position because now we skipped this part and we started looking from here for Peter and it gives us 4 it still will give you the index from the beginning of your array so 0 1 2 3 4 even though we told it to search starting from the middle it's still returning the position from the beginning of the array most of the time you're probably not gonna have that optional starting number but you could so let's do name start so the next thing I'm gonna talk about this join so if I do join let's just create a new variable and I would put that again join is not gonna modify your array so that's gonna be text so if I look at a log basically what it does it combines all the elements in the array with a comma and it creates a piece of text string now what you can do with Joe and you can provide your own separator here so if I do something like this if I run this see now I joined those pieces of the array to a string that have this space - space in the middle and then we can also for example do this so I'm leaving the area between that parenthesis with two quotes empty which means basically just leave it blank so by default is gonna use a comma but you can modify that by providing your own character here so a dash would be fine now what I want to do I want to kind of talk about this methods all over again by incorporating multi-dimensional or so I'm gonna create two dimensional array
Info
Channel: Learn Google Spreadsheets
Views: 51,333
Rating: undefined out of 5
Keywords: JavaScript, Arrays, Methods, Google Sheets, Apps Script, Tutorial, range to array, array to range
Id: tZdSa6p3PHQ
Channel Id: undefined
Length: 15min 12sec (912 seconds)
Published: Sat Oct 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.