Multi-Dimensional Arrays in JavaScript

JavaScript
Transcript

English (Auto-generated)

Hi everyone and welcome to this. Cast on Multidimensional arrays now, a multidimensional array is defined as an array containing other arrays. And javascript natively does not support Multidimensional arrays like some other languages, for example, C c plus plus or java. So in order to create a multidimensional array in javascript, we have to insert an array inside of another array to make it multi dimensional. And therefore this will then be organized as a matrix of rows and columns as we'll see. So to see this, let's go ahead and first declare a multi dimensional array. And like I said, these multi dimensional or two D arrays can be created by nesting one array inside of another. Right? And multidimensional arrays are basically they defined the same way as a one D array is defined. So we'll just say let to the array is equal to open and close uh brackets. Now let's declare to other rays, which we will then nest inside of this two D array. So we'll say let info Array. one is equal to some characters And then we'll make another array. So let info Array two is equal to some rooms. So some strings here, an apple and then a strawberry. So taking care of syntax. Right? So we have to erase here. Let in four a one and let in four array to now, in order to make a multidimensional array, we will nest these 21. D arrays into the two D array. So we'll say well to the array retrieved already declared up here and this is actually the word too. D is equal to an array containing our one d array. So info array one and then info 32. So we've declared, we had declared an array earlier which is called a two D array and it was empty, initially empty. And we inserted to one D arrays into R two D array out here. So let's go ahead and then console dot log R two d array and see what's going on here. So control s to save and I'm gonna head over here and out here. It's think that it is empty. It doesn't seem correct. So let's go back. So two d array is equal to two D array is oops, okay, there you go. So spelling is wrong, You don't want to do that and now it should work. Perfect. There you go. So it's telling me that we have in roll number zero, a bps, these characters and then roll number one, I have apple, banana, pineapple and the string, strawberry and in each role I have four columns. So we've nested to one day arrays into another array, therefore making And a two dimensional array. Now you could have also done it another way, you could have done it all at once, for example. So coming back here, let's say I have led to dimensional array is equal to an array off a raise. So we could have done the same thing out here with this syntax which is going to get for us the same result and we do need a comma here if we're going to use this syntax and once again I'm just using the same strength the same string values to demonstrate this and strawberry. And let's comment this out and then let me go ahead here and console dot log e two dimensional array and then control us to save and coming back to our browser and in the console. There you go. So we've done the same thing but with a different syntax. So now we have uh you know, we still have two rows and each row has a four columns and we can also now Access the length property of a multidimensional array with the dark notation which is going to return to us the number of rows in the two D array. So out here, if I do console dot blog, two dimensional array, dot length, it is actually going to get for me the number of rows in a two d array. So let's go ahead and save this and coming back here, You'll see that we see the # two which actually refers to the number of rows. Now in order to query the data type you can either use uh the es five array is array method, which is going to return the boolean, true or false or you can also use basically the type of operator. So let's go ahead and query our array now out here if I do console dot log array is array which takes as an argument are to the array, it is either going to return the bullying true or false if this is an array. So basically what the array is array method is doing is that it's going to take the two D array as an argument and returns true if it is an array and we can also use the type of operator, which will return object because arrays are the, you know, object, data type. But this method is more specific because it's going to return boolean or true if it's if this is in a race. So let's go ahead and check this out and out here. If I do this, it returns to me, you know the bullying true because the method query if two dimensional array was an array, which it is. You can also use the type of operators out here, which is basically going to return to us um that object, right? Because arrays are the object data type. So let's go ahead and now cry this out and there you go. So it's returning to us. Um the type of operator operator returns object. Now let's come back. So in order to access items in a multidimensional array, we can use the square bracket notation indicating both the row and column number. So keep in mind that in arrays indexing starts at zero. So let's go ahead and now access the first item in the first row from the two d array, two dimensional array, which is this letter, this character A So the way to do that is well console console dot blog. And out here we're going to say two dimensional array. So the first row which is zero and at the first column which also starts at the number zero. So control? S to save this and let me go ahead and basically now comment out the other console logs so we we don't have to see all of those just focusing on what we're doing here. So out here, you see that I got the letter. So we're accessing the first item in rule number zero and column number zero, which is the character A. Now let's go ahead and in our two dimensional array let's access the string pineapple which is in roll number two, sorry one, it's the second row. So index one, since indexing starts at zero, so this is zero and this is one. So it's in roll number one and column to write. So 012. So let's go ahead and do this. So console dot log two dimensional array. And we're accessing role at Index one And we're going to go ahead and access the item at in in column two which is pineapple. And now control. S and if I go back here you will see I have a pineapple. So we have an uncaught reference area which is probably a typo let's see what this is. There you go. So this is a small typo let's just go ahead and delete that and come back here and refresh and go back to the console. There you go. So pineapple. Now pineapple is located In column and number two in rule one. Right, So this is roll one at index one and column two. And we can also iterate through to the arrays. And in order to iterate through a multidimensional array, we can use a nested for loop. Right? So let's go ahead and now loop over our two dimensional array and print out all the items in our array on a new line. So I'm gonna use a for loop here. So four. And out here led I is equal to zero and I is less than two dimensional array dot length. And I plus plus and out here I will declare a local variable. So that item is equal to two dimensional array at index I. So referring to the current item during iteration and now I am also, I'm going to use a nested for loops. So four. And let's just use K As convention. So let K is equal to zero and K is then less than item dot length and then K plus plus. And out here we will console dot log item at index K. So let's take a look at what we are doing out here. So the outer for loop is going to iterate through the nested arrays inside the two dimensional array. And inside the first for loop we've declared a local variable called item, which references and in an individual nested array. So item out here references these individual nested arrays. And the second for loop out here is going to iterate through each item in the individual nested array and log the item. So let's go ahead and now control us to save and check this out in the console and there you go. So out here, starting here, I have printed out or log to the console, each individual item in the nested arrays. And that's all for this cast. And I hope you found it useful to d arrays are incredibly useful for making web based games.
88 Views 0 Likes 0 Comments

Though not natively supported in JavaScript, we can use multi-dimensional arrays in JS. This cast is a brief introduction to 2D arrays.

Comment
Leave a comment (supports markdown format)