3.3 Array Slicing - Video Tutorials & Practice Problems
Video duration:
4m
Play a video:
<v Instructor>In addition to the bracket notation</v> for selecting elements that we saw in the last section, JavaScript also supports a useful technique called array slicing that lets us select multiple elements at a time. We're gonna be talking about sorting arrays in the next section. So let's redefine our array to be purely numerical. Just remember what it is from the end of the last section. Can redefine it like this. You can say 42, 8, 17, and 99 is more or less randomly chosen. Not completely random. So there's our array. And we can use the slice method like this. We can give it an index. See if you can figure out what this is going to do. This actually depends on the language. Different languages have slices but their behaviors are different. In JavaScript, a.slice of one, you can see what it does is it goes to index one, which is eight, and then just selects the rest of the array. We can also slice from one index to the next, like this. A.slice one, three. See if you can guess. It's gonna go one, two, this is zero, one, two, is it going to pick off the 99 or not? It does not. So the way that JavaScript does this is it goes from the first index that's listed, so one in this case, and then stops at index three without selecting it. So if we wanted to get all of them, we could do like this. In fact, let's see what happens if we just go crazy. Yeah, so this actually selects all the way up to 40, even though it doesn't exist. And in fact, four doesn't exist either. It's just zero, one, two, three. And of course, if you want to ensure that you select to the end, you can do what we did here. And just give it one argument. One common task with arrays is selecting the final element. So there's a way to do this with the square bracket notation. If we do like this. A of three will give us the last element. But what if it's a really long array and we don't happen to know how long it is? Well, we can calculate it because arrays have a length property, just like strings. A.length is four. So the largest index is always the length minus one. That means that we can select the last element in the array, like this. A of a.length minus one. With a variable as short as the letter a, this isn't that bad but it gets kind of inconvenient when you have a longer variable name. So for example, suppose we have aMuchLonger name. We'll just assign it to a for convenience. So it's long enough that I'm not gonna type it out. I'm just gonna copy and paste. So you can see it's the same. If we want the last element of this one, well, we have to do this. It's the same thing. So you can see it's a little inconvenient. There's a convenient way to do this with array slicing though. Remember, array.slice of an index selects from that index to the end. You can actually go backwards too. Check this out. You can do slice -1. So this slice is from the end and selects the last element. So if you look at this carefully, the result of slice is itself another array, so this isn't exactly the last element. It's actually an array with one element, which is equal to the last element of the main array, this one. So in order to select that out, we could do this. One final case that's really common is wanting the last element of an array while removing it at the same time. And we'll talk about that in the next section.