Computer Science
Javascript Multidimensional Arrays
Javascript multidimensional arrays are arrays that contain other arrays as their elements. They are used to store and manipulate data in a tabular format, with rows and columns. Multidimensional arrays can be created and accessed using nested loops and indexes.
Written by Perlego with AI-assistance
Related key terms
1 of 5
3 Key excerpts on "Javascript Multidimensional Arrays"
- eBook - PDF
- Patrick Carey, Sasha Vodnik, Patrick Carey(Authors)
- 2021(Publication Date)
- Cengage Learning EMEA(Publisher)
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. EXPLORING HTML COLLECTIONS 83 would return values of 3 (the number of rows) and 4 (the number of columns). This assumes that every row has the same number of elements as the first row. You can continue to nest arrays in this fashion to create matrices of even higher dimensions. One reason to use multidimensional arrays is to match values from different arrays within a single variable. For exam- ple, the game data shown in Figure 3-3 could be placed within a single multidimensional array as follows: let games = [ ["2024-7-28","Bettendorf","h",2,1,9,"W"] , ["2024-8-1","Marion","a",4,2,9,"W"] , ["2024-8-2","Clinton","h",2,0,9,"W"] , ["2024-8-3","Clinton","h",1,5,6,"L"] , … Information on Tipton’s second game would come from the entries in the second element of the games array with the date given by the expression games[1][0], the opponent given by games[1][1], the location by games [1][2], and so forth. As you become more comfortable with arrays, you might find it easier to place your data within a single multidimensional array rather than spread across several arrays. Exploring HTML Collections The Document Object Model organizes HTML elements into collections where each element is an HTML Collection Object. For example, all hyperlinks are part of a collection of links, all input controls are part of a collection of form elements, and so forth. Though these collections are not arrays, they share many of the features of arrays. Figure 3-4 lists the JavaScript properties and methods used to access HTML collections within the DOM. - eBook - PDF
- Chris Minnick(Author)
- 2022(Publication Date)
- For Dummies(Publisher)
Understanding Multidimensional Arrays Not only can you store arrays inside of arrays, you can even put arrays inside of arrays inside of arrays. This can go on and on. An array that contains an array is called a multidimensional array. To write a mul- tidimensional array, you simply add more sets of square brackets to a variable name. For example: let listOfLists[0][0]; Multidimensional arrays can be difficult to visualize when you first start work- ing with them. Figure 4-2 shows a pictorial representation of a multidimensional array. Understanding Arrays CHAPTER 4 Understanding Arrays 217 You can also visualize multidimensional arrays as hierarchal lists or outlines. For example: Top Albums by Genre 1. Country 1.1 Johnny Cash:Live at Folsom Prison 1.2 Patsy Cline:Sentimentally Yours 1.3 Hank Williams:I’m Blue Inside 2. Rock 2.1 T-Rex:Slider 2.2 Nirvana:Nevermind 2.3 Lou Reed:Transformer 3. Punk 3.1 Flipper:Generic 3.2 The Dead Milkmen:Big Lizard in my Backyard 3.3 Patti Smith:Easter Here is a code that would create an array based on Figure 4-2: let bestAlbumsByGenre = [] bestAlbumsByGenre[0] = "Country"; bestAlbumsByGenre[0][0] = "Johnny Cash:Live at Folsom Prison" bestAlbumsByGenre[0][1] = "Patsy Cline:Sentimentally Yours"; bestAlbumsByGenre[0][2] = "Hank Williams:I'm Blue Inside"; bestAlbumsByGenre[1] = "Rock"; bestAlbumsByGenre[1][0] = "T-Rex:Slider"; bestAlbumsByGenre[1][1] = "Nirvana:Nevermind"; bestAlbumsByGenre[1][2] = "Lou Reed:Tranformer"; bestAlbumsByGenre[2] = "Punk"; bestAlbumsByGenre[2][0] = "Flipper:Generic"; bestAlbumsByGenre[2][1] = "The Dead Milkmen:Big Lizard in my Backyard"; bestAlbumsByGenre[2][2] = "Patti Smith:Easter"; 218 BOOK 3 Advanced Web Coding Accessing Array Elements You can access the elements of arrays in the same way that you set them, using square brackets and the index number. - eBook - ePub
JavaScript from Beginner to Professional
Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages
- Laurence Lars Svekis, Maaike van Putten, Rob Percival, Laurence Svekis, Codestars By Rob Percival(Authors)
- 2021(Publication Date)
- Packt Publishing(Publisher)
["Bananas", "Carrots", "Lettuce", "Eggs", "Milk", "Juice", "Pop", "Juice", "Pop"]Multidimensional arrays
Earlier, we established already that arrays can contain any data type. This means that arrays can also contain other arrays (which, in turn, can contain… other arrays!). This is called a multidimensional array. It sounds complicated, but it is just an array of arrays: a list of lists:let someValues1 = [1 , 2 , 3 ]; let someValues2 = [4 , 5 , 6 ]; let someValues3 = [7 , 8 , 9 ]; let arrOfArrays = [someValues1, someValues2, someValues3];So, we can create an array of already existing arrays. This is called a two-dimensional array. We can write it like this:let arrOfArrays2 = [[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]];If you want to access elements of the inner arrays, you will have to specify an index twice:let value1 = arrOfArrays[0 ][1 ];The statement will grab the first array because it has an index position of 0 . From this first array, it will take the second value, because it has an index position of 1 . Then it stores this value in value1 . That means the value of value1 will be 2. Can you figure out what the value of the next one will be?let value2 = arrOfArrays[2 ][2 ];It takes the third array, and from this third array, it takes the third value. Thus, 9 will be stored in value2 . And it does not stop here; it can go many levels deep. Let's show that by creating an array of our array of arrays. We are simply going to store this array three times in another array:arrOfArraysOfArrays = [arrOfArrays, arrOfArrays, arrOfArrays]; This is what the array looks like in terms of values: [ [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ], [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ], [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ] Let's get the middle element of this array, which is the value 5, belonging to the second array of arrays. It is done like this:let middleValue = arrOfArraysOfArrays[1 ][1 ][1 ];The first step is to get the second array of arrays, so index 1. Then we need to get the second array of this one, which again is index 1. Now we reach the level of the values, and we need the second value, so again we use index 1. This is useful in many situations, for example, when you want to work with matrices.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.


