Return to Kroll Design home page

JavaScript Tutorials

 
« Return

multi-dimensional named arrays

Multi-dimension arrays are arrays where each element is itself an array.
When referencing a named array element with a literal, make sure to put it in quotes:

Wrong: toothpaste[price] = 1.43;
Right: toothpaste["price"] = 1.43;

Code:

<script type="text/javascript">
//<![CDATA[
var floss = new Array(); floss["item_nbr"] = 12; floss["price"] = 4.99; floss["quantity"] = 345; var toothbrush = new Array(); toothbrush["item_nbr"] = 203; toothbrush["price"] = 2.77; toothbrush["quantity"] = 2416; var toothpaste = new Array(); toothpaste["item_nbr"] = 89; toothpaste["price"] = 1.43; toothpaste["quantity"] = 968; var items = new Array(); items["floss"] = floss; items["toothbrush"] = toothbrush; items["toothpaste"] = toothpaste; for (x in items) { for (y in items[x]) { document.write("items[", x, "][", y, "] : ", items[x][y], "<br>"); } document.write("<br>"); } //]]> </script>

Output:



 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM