1 00:00:01,250 --> 00:00:06,920 So now let's look at some selected methods of a race and we just picked a few. 2 00:00:07,280 --> 00:00:08,270 There are a lot more. 3 00:00:08,300 --> 00:00:13,040 And if you look at the Mozilla documentation, you'll find out about all of them. 4 00:00:14,360 --> 00:00:18,590 So two of the most popular methods for the arrays are push and pop. 5 00:00:19,520 --> 00:00:26,210 And what push does is pushes the value onto an array and pop or pop the value off the last value. 6 00:00:27,020 --> 00:00:31,070 So if we push 25 on and we pop, we'll get 25 back. 7 00:00:31,670 --> 00:00:34,220 And then there's some that may seem a little familiar from strings. 8 00:00:34,220 --> 00:00:42,110 For example, there's an index of where is the index of a value of 25 for the last index of you're going 9 00:00:42,110 --> 00:00:45,890 to reverse an array to turn it from front to back. 10 00:00:46,940 --> 00:00:49,330 Slice is a lot like the string slice. 11 00:00:50,240 --> 00:00:55,820 So it gets the values starting at position two and then prior to index four. 12 00:00:57,080 --> 00:00:59,020 And then you can do sorting with an array. 13 00:00:59,030 --> 00:01:00,330 And this is very important. 14 00:01:01,070 --> 00:01:05,480 So if you have an array of strings, you can use this method without doing anything else. 15 00:01:06,260 --> 00:01:09,680 So in my earlier example, we have an array of names. 16 00:01:09,680 --> 00:01:15,650 You can sort them alphabetically, but if you have numbers or something more complicated, you need 17 00:01:15,650 --> 00:01:17,450 to provide a function to do the sorting. 18 00:01:17,450 --> 00:01:19,460 And we'll talk about that in a later section. 19 00:01:20,670 --> 00:01:26,640 OK, I'm back in this array program that we started in the previous section, so it's going to come 20 00:01:26,640 --> 00:01:27,630 in kind of handy here. 21 00:01:28,350 --> 00:01:30,450 So let's do a couple of methods. 22 00:01:30,960 --> 00:01:40,680 So if I say push, I will say my array that push 100 and then let's log that out. 23 00:01:45,930 --> 00:01:48,780 And then I'm going to pop that out as well. 24 00:01:49,260 --> 00:01:55,410 So let's just repeat this, except for the very first thing I'm going to put on the log is the pop. 25 00:02:03,070 --> 00:02:06,580 So it's kind of Poppit, it's going to put out there and it's going to turn out the one. 26 00:02:12,280 --> 00:02:14,550 Let's come on a few things out. 27 00:02:21,290 --> 00:02:26,540 All right, so the first thing we do is we pushed 100 onto the array and now it's here at the end and 28 00:02:26,540 --> 00:02:31,180 then we popped it and you can see we got 100 off and now they're raised back to five in length. 29 00:02:31,730 --> 00:02:38,090 And if we keep popping, we're going to pop things off with pop more than we can get. 30 00:02:44,410 --> 00:02:49,920 But you can see we're getting one hundred eight six five four two two is the last thing, right? 31 00:02:49,930 --> 00:02:52,150 I don't think I'd consider it loud enough. 32 00:02:52,180 --> 00:02:58,410 I wanted to do it a couple more times just to see what happens when the array is empty. 33 00:02:59,740 --> 00:03:01,200 So when it's empty, we get undefined. 34 00:03:02,860 --> 00:03:05,250 So I said, OK, it's undefined, it's empty. 35 00:03:05,560 --> 00:03:08,200 Empty array in its length is zero. 36 00:03:09,970 --> 00:03:13,210 So no matter how many times you try to pop into your head, you're not going to get anything out of 37 00:03:13,210 --> 00:03:14,910 it, but you can then push again. 38 00:03:16,010 --> 00:03:21,970 Another problem there, if I take this push and put it down in here, we can push that that 100 back 39 00:03:21,970 --> 00:03:22,690 onto the tray. 40 00:03:27,370 --> 00:03:29,140 And so then we got one hundred off at the end. 41 00:03:31,170 --> 00:03:33,150 All right, let's try something else. 42 00:03:35,960 --> 00:03:40,340 At these out, we can try the index of. 43 00:03:41,940 --> 00:03:44,970 And we know we have an index of, say, number five. 44 00:03:47,020 --> 00:03:58,540 So we can say cancel that log, my array index of and you can see like some of the other ones, it has 45 00:03:58,540 --> 00:04:01,060 a search element and a from index. 46 00:04:01,070 --> 00:04:02,460 Where do you want to start searching? 47 00:04:03,340 --> 00:04:06,310 So we'll just say five won't specify from index. 48 00:04:13,620 --> 00:04:18,210 So we've got the number two, and if we scroll back up here, we can see that the five is a position 49 00:04:18,210 --> 00:04:20,100 to zero one to. 50 00:04:21,560 --> 00:04:22,460 So that's how I found it. 51 00:04:22,850 --> 00:04:24,380 How about the reverse? 52 00:04:24,380 --> 00:04:26,600 We won't worry about the last index except for right now. 53 00:04:29,360 --> 00:04:35,030 So if I say console that, log my array dot reverse. 54 00:04:42,600 --> 00:04:44,520 And so eight, six, five, four, two. 55 00:04:46,550 --> 00:04:53,330 In the era, did it change the era, let's add that to the log. 56 00:05:01,950 --> 00:05:02,870 Yeah, it did. 57 00:05:04,150 --> 00:05:08,230 We have to be careful about which ones are using some of these will return a new array and some of them 58 00:05:08,230 --> 00:05:12,170 will change the array itself, like push changes the array. 59 00:05:13,840 --> 00:05:22,090 So one last one will try is the sort so we can use the sort method, but it's going to need something 60 00:05:22,090 --> 00:05:23,810 to actually make it sort correctly. 61 00:05:24,820 --> 00:05:25,780 So let me demonstrate. 62 00:05:25,780 --> 00:05:27,320 How can so it looks right. 63 00:05:28,210 --> 00:05:35,080 So I'm going to say my array and I'm going to put in some strings and I'll say a. 64 00:05:36,250 --> 00:05:37,810 I start with something like lower 65 00:05:40,520 --> 00:05:42,400 Cafcass square brackets. 66 00:05:46,840 --> 00:06:01,180 And then, as I says, you you a with quotes and we'll leave it there. 67 00:06:02,500 --> 00:06:05,470 So now I say, Meyera, that sort, 68 00:06:08,260 --> 00:06:09,790 and we want to put it up. 69 00:06:22,730 --> 00:06:28,040 So we've got our very nicely sorted ways and then the ends and the has been used and disease. 70 00:06:28,880 --> 00:06:34,010 So we run into problems though is if we have numbers in here, because numbers in the sort of algorithm 71 00:06:34,010 --> 00:06:35,870 are treated like strings. 72 00:06:36,680 --> 00:06:40,880 And so sorting numbers is going to work very well demonstrated. 73 00:06:45,210 --> 00:06:47,260 Versus changes back to a number array. 74 00:06:48,700 --> 00:06:56,290 We'll say one thousand or one hundred thirty ten. 75 00:06:57,820 --> 00:07:01,900 Fifteen hundred, three hundred and thirty three. 76 00:07:02,830 --> 00:07:03,300 Twelve. 77 00:07:04,240 --> 00:07:06,430 OK, so let's see what happens when we start that array. 78 00:07:12,310 --> 00:07:18,310 So now we get an order that we're not expecting because it's sought by the as if it's a string, so 79 00:07:18,310 --> 00:07:25,600 sorting based upon the first character going left to right, so says, OK, the ones come first and 80 00:07:25,600 --> 00:07:26,550 then the threes. 81 00:07:27,370 --> 00:07:32,790 But you see the one zero comes before one too, which comes before one five. 82 00:07:33,550 --> 00:07:36,160 So the numeric value of the number doesn't matter. 83 00:07:36,170 --> 00:07:36,990 It's only the string. 84 00:07:37,450 --> 00:07:43,090 So the sort numbers we have to do something more or in the next two sections we're talking about the 85 00:07:43,090 --> 00:07:47,860 spread operator and de structuring arrays and this is a little more of an advanced topic. 86 00:07:47,870 --> 00:07:52,030 So you could skip and come back to it or you could go through it now and come back to it later. 87 00:07:52,900 --> 00:07:54,610 But it's something you definitely need to practice.