Multidimensional Array in JavaScript

Photo by Faris Mohammed on UnsplashA multidimensional array is not available natively in JavaScript, to have a multidimensional array we have to create an array of arrays.A multidimensional array holds a matrix of elements.In this example, we will use …


This content originally appeared on Level Up Coding - Medium and was authored by Monica A.

Photo by Faris Mohammed on Unsplash

A multidimensional array is not available natively in JavaScript, to have a multidimensional array we have to create an array of arrays.

A multidimensional array holds a matrix of elements.

In this example, we will use a car showroom, where the car is the element that is parked and arranged like a grid with rows and columns.

The car showroom will be a multidimensional array of separate arrays, one array for each row within our grid is a matrix.

To access an element from a multidimensional array you need two indexes like the example below.

But it’s not practical if we have lots of elements in our array to console.log them one by one. For that reason, we can use ‘for loop’ and ‘nested for loop’, which is a loop inside of a loop

If we just use the outer loop below:

The output will be an array of each array:

To display each of the elements separately, we need to use a nested loop:

The output will be:

Lastly, we can also display the indexes along with each of the elements:

By doing so we got to see the coordinates where the elements are located.

There you have it, the multidimensional array in JavaScript…

Happy coding!


Multidimensional Array in JavaScript was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Monica A.


Print Share Comment Cite Upload Translate Updates
APA

Monica A. | Sciencx (2022-03-15T12:31:06+00:00) Multidimensional Array in JavaScript. Retrieved from https://www.scien.cx/2022/03/15/multidimensional-array-in-javascript/

MLA
" » Multidimensional Array in JavaScript." Monica A. | Sciencx - Tuesday March 15, 2022, https://www.scien.cx/2022/03/15/multidimensional-array-in-javascript/
HARVARD
Monica A. | Sciencx Tuesday March 15, 2022 » Multidimensional Array in JavaScript., viewed ,<https://www.scien.cx/2022/03/15/multidimensional-array-in-javascript/>
VANCOUVER
Monica A. | Sciencx - » Multidimensional Array in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/15/multidimensional-array-in-javascript/
CHICAGO
" » Multidimensional Array in JavaScript." Monica A. | Sciencx - Accessed . https://www.scien.cx/2022/03/15/multidimensional-array-in-javascript/
IEEE
" » Multidimensional Array in JavaScript." Monica A. | Sciencx [Online]. Available: https://www.scien.cx/2022/03/15/multidimensional-array-in-javascript/. [Accessed: ]
rf:citation
» Multidimensional Array in JavaScript | Monica A. | Sciencx | https://www.scien.cx/2022/03/15/multidimensional-array-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.