Multi-Dimensional Arrays in JavaScript (Part 2)

JavaScript
Transcript

English (Auto-generated)

Hi everyone. And let's continue our discussion on a two D arrays in Javascript. Now we can use methods such as pop push index, soft shift roost sort on a multidimensional arrays. So let's try a few of these. And this is not much different from using these methods on one day arrays. So let's go ahead and try the Pop method on our two dimensional array. Now the pop method is going to pop off or remove the last item in our two d array and therefore decreasing the length of our multidimensional array. So let's go ahead and try this off. Try this. So we have two dimensional array. God pop. And now if I go ahead and console dot log two dimensional array, you will see that it has popped off the last array in our two dimensional array, which is this array here with these string values apple, banana, pineapple, and strawberry. So control s to save. And let's check this out and out here, it's telling you. Well, there's an array and we have one a nested array in r two d array with these values a bps. So we've popped off the last array, which is this one out here. Now. Similarly, just as you can pop, you can also push items to our to the array. So for example, let's go ahead and add items to this multidimensional array, which is going to insert an array at the last index. And what I'm going to do out here is I'm just going to comment out the part where we popped so that our original remains intact. And now let's go ahead and try to push and I'm going to say, well two dimensional array dot push. So using the push method and groups have a typo so I'm going to push in a bunch of numbers and once again, as usual, just verifying the results with a console log and control s to save and let me just push this down a bit and control are and there you go. So now it is letting us know that I have within my ray, I have um at index zero. Well there's something going on so let's go ahead and try this out. So what's going on here is basically this has to be a nested array. So I am actually pushing a nested array within my two dimensional array. So now if I go ahead and refresh, this is going to give us the expected answer. Which is basically we have a nested array at index zero and then the array at index one. And then the array that we just inserted using posh push method at index two. So what I was not doing out here, which originally was I did not make this an array. I wasn't pushing an array. I was just pushing some values and each value had its own index but now we have a multi dimensional array with these three rows inserted inside our main two D array. Now, besides push and pop, which are, you know, very common methods. We can also use other methods, for example, we can use the index index of method, which is going to return the index of an argument that is being queried for within an array. And then we can either iterate through the two D or multidimensional array or flatten it and then use the index of method. So let's go since we've already gone over iterating multidimensional arrays, let's actually try flattening a to the array. So let's flatten the array and find the index of let's say uh the string Apple here. Okay, so let's go ahead and flatten our array. And what I'm going to do out here is basically I'm also going to now comment this out and comment this will let this be So out here, I'm going to use index of method and flat. Right, so let's flatten R two D array. And the way that we're going to do that is we're gonna say, well let flatten array is equal to Right, two dimensional array and got a flat method. So let's go ahead and try this out. And what this is actually going to do is that it's going to return a flattened array with the array items inside of it. So let's see this And out here let's see what's happening here first. So let's flatten array. There you go. I'm not even putting the correct name and now. Okay, okay, so it is showing the correct thing, but what I'm intending to do is to console log the flattened array. Right, So I probably should have used a different name. There you go. So now we have the flattened array, which means that we have every value from R2D arrays flattened and returned to us in a single array. And now we can use the index of method which should return to us the index of the string. Apple If found. And if the queried item in an array is not found, it's just going to return to us -1. So let's go ahead and try this out. And now I'm going to say, well let index is equal to and let me just move this down. So let index is equal to flatten array dot index of method. So I'm using the index of method to find the index of the string Apple in my flattened array. So let's go ahead and then console dot log index. So control s to save and let's try this out and there you go. So it lets me know that the index of the string Apple, which is referenced by the variable index is at position number four. So we have a at zero and then B p s. And here we can see that we have Apple at index four. So besides using index of method, you can actually also use a splice method as well on your two D array. So what is the splice method doing now the splice method altars and array by adding or removing an item from an array and this is going to alter the original array. So the splice method does mutate the original array, and it's going to take the specified index of the item you want to remove from or add to, and it'll also take as an argument how many items you want to remove? So these are the arguments you would pass to the splice method. Additionally, you can also pass as an argument the items you want to add to the ray. So let's go ahead and actually try this out. So what I'm going to do out here is once again, I'm just going to comment this out. So at every point you're seeing these examples with the original one D arrays inserted inside r two D array. Right? So it's got at index zero this nested array and at index one or row one inside the two dimensional array. You have this secondary nested. So let's go ahead. And now actually try let's add another array to to our two dimensional array at index two. So We can say two dimensional array. God. And then using the splice method. And we and this is going to take as arguments where we want to add our new array, which is at Index two. And how many arrays we want to add. Well, I just want to add one array and then the actual array that we want inserted. So out here, I'm just going to um have some string values. Right, okay, so pretty much everything to do with coffee right now. Right. So now what we're doing out here is basically we are adding at index 21 nested array, which is what we mean by these two arguments. And then the nested array, is this array out here with the string values. So let's go ahead. And then consul dot blog are two dimensional array. Just to verify that we have indeed added an array in our two dimensional array using splice And there you go. So now are two dimensional array contains an extra nested array at Index two. And these are the values of the array. Now, similarly, we can also use the splice method to remove items from the two dimensional array. So for example, let's suppose that we're going to remove one item from the first row. So we're going to now remove this array and coming back here. What we can do is so let's use a splice to remove a nested array. So let's go ahead and say, well, two dimensional array, I'm going to use the splice method. And what I'm going to do out here is I'm going to remove the first nested array. So at rose zero and I'm removing just one item and the item out here actually refers. Two. This nested array at index zero. So once again, let's verify our results with a console dot log two dimensional array and there you go. So coming back here refresh and there you go. So now you see that the previous previously we had uh we had nested this array, right? So we've removed this array, a Bps, which was the first array in r two D array at index zero. So we've removed the first item in our two dimensional array, which is at index zero. So that's all for this cast, which was about two D arrays. And certainly there are more methods that you can use on tune dimensional arrays. For example, you can use the shift method, reverse method sort. So it's not very much different from one D arrays. You just have to keep in mind that in this, in two D arrays, you're dealing with rose and columns of items.
138 Views 0 Likes 0 Comments

A bit more about manipulating 2D arrays in JavaScript using array methods.

Comment
Leave a comment (supports markdown format)