Javascript 3 Dimensional Arrays

Javascript as far as i know doesn’t natively support 3 dimensional arrays. This is more of an annoying hack than anything, 3d arrays in javascript require arrays of arrays of arrays.

In this example i will be using some predefined data, in practical use you will most likely be looping through several sets of dynamic data. Have fun.


var threeArray(); // top level array, first place set.

init();
alertSomeData(4,3,0); // simple function returning data from threeArray.

function init()
{

cat1Array = new Array();

   product1Array = new Array("item1","item2","item3");
   product2Array = new Array("item1","item2","item3");
   product3Array = new Array("item1","item2","item3");
   product4Array = new Array("item1","item2","item3");
   product5Array = new Array("item1","item2","item3");

 // pushing arrays,to more arrays
cat1Array.push(product1Array,product2Array,product3Array,product4Array,product5Array);

cat2Array = new Array();

   product1Array = new Array("item1","item2","item3");
   product2Array = new Array("item1","item2","item3");
   product3Array = new Array("item1","item2","item3");
   product4Array = new Array("item1","item2","item3");
   product5Array = new Array("item1","item2","item3");

cat2Array.push(product1Array,product2Array,product3Array,product4Array,product5Array);

cat3Array = new Array();

   product1Array = new Array("item1","item2","item3");
   product2Array = new Array("item1","item2","item3");
   product3Array = new Array("item1","item2","item3");
   product4Array = new Array("item1","item2","item3");
   product5Array = new Array("item1","item2","item3");

cat3Array.push(product1Array,product2Array,product3Array,product4Array,product5Array);

cat4Array = new Array();

   product1Array = new Array("item1","item2","item3");
   product2Array = new Array("item1","item2","item3");
   product3Array = new Array("item1","item2","item3");
   product4Array = new Array("item1","item2","item3");
   product5Array = new Array("item1","item2","item3");

cat4Array.push(product1Array,product2Array,product3Array,product4Array,product5Array);

cat5Array = new Array();

   product1Array = new Array("item1","item2","item3");
   product2Array = new Array("item1","item2","item3");
   product3Array = new Array("item1","item2","item3");
   product4Array = new Array("item1","item2","item3");
   product5Array = new Array("item1","item2","item3");

cat5Array.push(product1Array,product2Array,product3Array,product4Array,product5Array);


// remember threeArray, his turn.

threeArray.push(cat1Array,cat2Array,cat3Array,cat4Array,cat5Array);

}


alertSomeData(i, e, o);
{
alert(threeArray*[e][o]); // now you know half of your vowels.
}


This [] first place value is the category, the 2nd [] is the product, and the last [] is the actual data. find a line similer to this

   product1Array = new Array("item1","item2","item3");

and replace the items within quotes with actual data, numbers, strings, objects whatever.

For this example, this line of code

alert(threeArray[2][5][0]);

would return category 2, item 5, and data set 1. The actual data returned would be “item1”.

Sorry if that didn’t make sense or there are any inconsistencies, correct me if i’m wrong somewhere.