Home arrow JavaScript arrow Page 2 - JavaScript Arrays
JAVASCRIPT

JavaScript Arrays


Arrays in JavaScript are extremely useful for storing and manipulating information you have coded directly into the script, or information collected from the browser. In this article, Dan Wellman details how to use and manipulate them.

Author Info:
By: Dan Wellman
Rating: 3 stars3 stars3 stars3 stars3 stars / 110
May 25, 2005
TABLE OF CONTENTS:
  1. · JavaScript Arrays
  2. · Multidimensional Arrays and Use
  3. · Beginning Code
  4. · Associating Images and Links
  5. · What to Put into the HTML

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JavaScript Arrays - Multidimensional Arrays and Use
(Page 2 of 5 )

Multidimensioned Arrays

In the same way that arrays contain integers or strings as values, multidimensional arrays contain arrays as their values. The syntax for a multidimensional array is as follows:

myArray = new Array([2,3,4], [5,5,9], [8,6,1])

To display the first item of the first array (which has an index of 0,0) you would need to use:

document.write(myArray[0][0])

This is known as a two-dimensional array, even though there are three arrays each with three values, as the highest index is 2,2. Referencing each of the items in multidimensional arrays can be cumbersome and confusing, and I have found it easiest to keep arrays singular and just have more of them as necessary, i.e. have three separate arrays rather than one two-dimensional array.

Finding Uses for Arrays

To show you how useful arrays are when you need to store and retrieve information, I have put together an example that demonstrates their power and usefulness. Imagine that you have an online store selling computer components, and you offer five products out of five of your product lines as special offers every week. Obviously there is only so much information you can fit onto your homepage, so you can’t show all 25 of the week’s special offers at once. But because you want to alert people that these offers exist, you need something on the homepage.

You decide that a random image generator would be your best use of space, and you want it to display one offer from each of the product lines at a time. You want each of the images to be a roll-over, the staple of any wholesome website. Each of the images should also link to a new page, giving the full description and details of each of the products. All of a sudden there is an incredible amount of information that your script is going to need to use; an array is going to be needed to keep track of it -- several arrays in fact.

This is a basic example of how you might put together the special offers box:

We’ll put the script in its own .js file for simplicity, so open up your favorite text editor and move on to the next page.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials