1. Tech

Your suggestion is on its way!

An email with a link to:

http://javascript.about.com/library/blarraym.htm

was emailed to:

Thanks for sharing About.com with others!

Multiple Dimensional Arrays

clr gif
Join the Discussion

Questions? Comments?

Javascript only allows us to create single dimensional arrays. Such an array can be created in a number of ways.

var a = new Array();
var b = new Array('a','b');
var c = new Array(5);
var d = ['a','b','c'];

The first of these examples creates a single dimensional array with no entries in the array, those would need to be added later. The second example creates an array with two entries b[0]='a' and b[1]='b'. The third example creates an array of five entries c[0] throug c[4] with all being set to null. The fourth example creates an array of three entries with d[0]='a' d[1]='b' and d[2]='c'.

So if Javascript can't create multiple dimensional arrays then how can we handle things when we need more than one dimension to our array? Well Javascript doesn't place any limit on what types of elements we can place in our array. If we put another array into each of the elements of an array then we end up with the equivalent of a multi dimensional array although the way that we need to access the individual elements is not quite as straightforward as it would be with a proper multi dimensional array.

As an example of an array of arrays let's consider this one from my Piano Chord Calculator.

var numcode = [['B1',0],['1',1],['#1',2],['B2',2],['2',3],['#2',4],
['B3',4],['3',5],['#3',6],['B4',5],['4',6],['#4',7],['B5',7],['5',8],
['#5',9],['B6',9],['6',10],['#6',11],['B7',11],['7',12],['#7',13],
['Cb',0],['C',1],['C#',2],['Db',2],['D',3],['D#',4],['Eb',4],['E',5],
['E#',6],['Fb',5],['F',6],['F#',7],['Gb',7],['G',8],['G#',9],['Ab',9],
['A',10],['A#',11],['Bb',11],['B',12],['B#',13]];

I have used the shorter [] notation for defining all of the arrays in order to save space. The outer [] around the entire code after the = defines an array of 42 entries (that can be referenced as numcode[0] through numcode[41]. Each of these entries is in turn an array (as indicated by the inner []. Each of these inner arrays consists of two entries. These entries can be referenced as numcode[i][0] and numcode[i][1] (where i is the value of the particular entry within the outer array that we are looking at).

One of the advantages of this approach using arrays of arrays rather than proper milti dimensional arrays is that each of the inner arrays does not need to be the same length as the others. It depends of course on exactly what you are using the arrays for as to what makes the most sense so often you will have situations where each of the inner arrays will be the same length like I have here.

With this particular array what I am using it for is to perform lookups of codes entered into another array and matching them to the [0] position entries. The [1] position entries are then used in the subsequent processing (ie it is a lookup table for translating the first field into the second).

Here is the actual code that I use to perform the lookups from the a[] array in order to then call a chg() function with each matched result.

for (var i=a.length -1; i >= 0; i--) {
for (var j = numcode.length - 1; j >= 0; j--) {
if(a[i] == numcode[j][0]) chg(numcode[j][1]+11,c);
}}

The reverse order processing is done because it means that the length of each array only needs to be calculated once and can be done because in this instance the order in which the entries in each array is processed doesn't matter.

Related Video
Make a PowerPoint Multiple Choice Quiz

©2014 About.com. All rights reserved.