Home arrow JavaScript arrow Page 4 - An in depth discussion of JavaScript Arrays
JAVASCRIPT

An in depth discussion of JavaScript Arrays


This series of articles mainly concentrates on working with JavaScript arrays. We shall start with the basics of JavaScript arrays and finally conclude with complex object based arrays in JavaScript. You can reuse these scripts to inject into server side controls easily (especially in .NET and Java).

Author Info:
By: Jagadish Chaterjee
Rating: 4 stars4 stars4 stars4 stars4 stars / 14
February 28, 2006
TABLE OF CONTENTS:
  1. · An in depth discussion of JavaScript Arrays
  2. · Working with single dimensional arrays: discussion
  3. · Working with two dimensional arrays
  4. · Working with two dimensional arrays: discussion
  5. · Another way to work with two dimensional arrays

print this article
SEARCH DEVARTICLES

An in depth discussion of JavaScript Arrays - Working with two dimensional arrays: discussion
(Page 4 of 5 )

Within the code in the previous section, I mainly created a simple button (which is identified as “ButtonShow”).  The button is defined with an “onclick” event which calls a JavaScript function “ButtonShow_onclick”, which is defined as follows:

function ButtonShow_onclick() {
Show();
} 

The above function simply calls another JavaScript function named “Show.”  The function “Show” is defined as follows:

function Show()
{
      var ITArray = new Array(2300, 3105, 2909, 4800);
      var ProductionArray = new Array(1800, 1940, 2470, 4350);
      var ResearchArray = new Array(900, 1200, 1923, 3810);
      var salaryArray = new Array(ITArray, ProductionArray, ResearchArray);

      for (var i = 0; i < salaryArray.length; i++) {
            for (var j = 0; j < salaryArray[i].length; j++) {
                  document.write(salaryArray[i][j] + "\t");
            }
            document.write("<br>")
      }
}

Before starting this discussion, we need to understand what we are creating (in memory) from the above code.  Let me provide you a logical view for the array (“salaryArray”) in the above code. Consider the following figure (Fig 01):

Just consider the above figure as a small part of memory identified by “salaryArray.”  All the black numbers correspond to values.  All the reds are access notations (the positions) for the values given in black.  Every access notation is a combination of a row index (in blue) and a column index (in green).

We are trying to create three single dimensional arrays as part of the main array, and thus we describe the main array as a “two dimensional” array (which is very similar to the concept of matrices in mathematics). 

According to the above code, the three single dimensional arrays (or rows) are “ITArray”, “ProductionArray” and “ResearchArray.  All of them have their own values (but the number of values in all of them is generally the same).  We created a main array named “salaryArray” which contains the three arrays in the form of a list (thus forming a table type of view).

Finally, we retrieve and display all the values in the “salaryArray” with the following nested loop:

      for (var i = 0; i < salaryArray.length; i++) {
            for (var j = 0; j < salaryArray[i].length; j++) {
                  document.write(salaryArray[i][j] + "\t");
            }
            document.write("<br>")
      }

With the above code fragment, the variable “i” corresponds to the “row index” and the variable “j” corresponds to the “column index.”


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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