Leo Umesh avatar

Multidimensional Array In JavaScript

leoumesh

Published: 21 Jan 2022 โ€บ Updated: 21 Jan 2022Multidimensional Array In JavaScript

Multidimensional Array In JavaScript

image.png


Image Source

In JavaScript, there is no any built-in support for working with multidimensional array. However, you can work wit multidimensional array by defining an array inside another array. You can use the following syntax if you want to access any value inside such array: array_name[x][y]. In this post, we will see an example of displaying items in a two dimensional array. Its just an array of my favourite dishes โ€Ž๐Ÿ˜ƒ. In order to display each element, we will use a nested for-loop. If you are working with a 3D array then you will need 3 for-loops to display the value and so on. In real life projects working with multidimensional array is not really applicable expecially when the size of array is very large. And we use objects to work with such scenarios.

<!DOCTYPE html>
<html>
<head>
<title>Multidimensional Array</title>
</head>
<script>
    var dish = [
        ["Crunchy Chicken", "Chicken Lollipop", "Chicken Roast", "Chicken Drumstick"],
        ["Kadai Paneer", "Palak Paneer", "Paneer Tikka", "Paneer Makhani"],
        ["Potato Stick", "Aloo Chop", "Aloo Dum", "Potato Chips"]
    ]
    document.write("Dishes: " + "

"
); for (var i=0; i<4; i++) { for (var j=0; j<4; j++) { document.write("[" + i + "]" + " " + "[" + j + "]" + "=" + dish[i][j] + "
"
); } document.write("
"
); } </script> <body> </body> </html>

If you run the above code in your browser then you can see the name of dish along with the 2D-array index.

image.png

Leave Multidimensional Array In JavaScript to:

Written by

I am just a passionate blogger.

Read more #hive-163521 posts


Best Posts From Leo Umesh

We have not curated any of leoumesh's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Leo Umesh