Working with Array of Arrays in JavaScript

 

If you have worked with languages like C, C++ or C# then you must be aware with the concept of multidimensional array. Multidimensional array implies arrays of array. In a two dimensional array, there would be rows and columns. A 3*5 two dimensional array can be shown as below,

0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
3,0 3,1 3,2
4,0 4,1 4,1

Even though you cannot declare a multidimensional array directly in JavaScript though you can have Arrays of Array in JavaScript.

You can define a two dimensional array as following in JavaScript

 

image 

In above code

  • First we defined a base array
  • Then we created two chid arrays. These arrays will constitute different rows of the base array.
  • Each child array is of different size.
  • Assigning each chid array as one of the rows of base array. 

In last line of above code listing, we are printing value of 1st row and 4th column of base array. We will get output as following,

image 
On noticing above code listing you will find that chid array is of different size. In JavaScript chid arrays can be of different size. Essentially you can have three dimensional array in JavaScript as well. You can define that as following code listing,

image

In above code listing,

  • We are defining array named grandparents. This is the base array.
  • We are defining array named parents. Grandparents can have parents.
  • We are defining array names child. Parents can have child.

We want to print “steve”. Steve is second element of child2 array. child2 array is second element of parents1 array. parents1 array is first element of grandparents array. so we can fetch steve with following index value on grandparents array

image 

As output you will get steve printed as following,

image 
For your reference code of two dimensional array and three dimensional array in JavaScript is given below,


<script type="text/javascript" >
 //Two Dimensional Array
 var baseArray = [];
 var chidArray1 = ["a", "b", "c", "d", "e"];
 var chidArray2 = ["1","2","3","4","5","6","7","8"];
 baseArray[0] = chidArray1;
 baseArray[1] = chidArray2;
 console.log(baseArray[0][3]);
 //Three Dimensional Array
 var grandparents = [];
 var parents1 = [];
 var parents2 = [];
 var child1 = ["john", "ram"];
 var child2 = ["mona", "steve", "mark"];
 var child3 = [];
 var child4 = ["chris"];
 parents1[0] = child1;
 parents1[1] = child2;
 parents2[0] = child3;
 parents2[1] = child4;
 grandparents[0] = parents1;
 grandparents[1] = parents2;
 console.log("printing Steve");
 console.log(grandparents[0][1][1]);

</script>


In this way you can work with Arrays of Array in JavaScript. I hope you find this post useful. Thanks for reading.

 

3 thoughts on “Working with Array of Arrays in JavaScript

  1. Pingback: What is Associative Array in JavaScript? « debug mode……

  2. Pingback: Dew Drop – November 14, 2012 (#1,442) | Alvin Ashcraft's Morning Dew

  3. Pingback: Blog Posts of the Week (11th - 17th November 2012) - The South Asia MVP Blog - Site Home - TechNet Blogs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s