Solved

Create a dynamic two dimensional array in Javascript

Posted on 2002-05-21
6
3,311 Views
Last Modified: 2008-01-16
Hi,

I am trying to create a dynamic two dimensional array in JavaScript.

For example I know that I need 6 columns - it's not even necessary to put this in if it's possible to do without.

I know that it's simple to create a one dimensional array as follows:

var myArray=new Array();

However, to create a two dimensional array - the only thing I can find on the net seems to want me to specify the size of it as follows:

var myArray=new Array();
for(i=0; i<3; i++)
   myArray[i]=new Array()

However, I don't want to have to put it into a limited loop - I want it dynamic.

Would appreciate any suggestions forthcoming!!!

Regards.
0
Comment
Question by:NoelleBlaney
6 Comments
 
LVL 1

Expert Comment

by:estrauss
ID: 7024495
Take a look at
http://www.promega.com/ctx/program/
and
http://www.promega.com/cellprolif/program/

I think that they both do what you want and are pretty easy to understand. (And I wrote them)

Ethan
0
 

Author Comment

by:NoelleBlaney
ID: 7024556
In your example, you have a set size for your array. You are specifying the row size for your array by using the following:

var ref = new Array();
And then referencing the elements in the array by saying ref[1]= whatever data you want to put into it.

I want to be able to do the following:

var myArray=new Array();
for(i=0; i<3; i++)
  myArray[i]=new Array()

but without specifying the 3 in the loop - I want this size to be dynamic - obviously it doesn't have to be in a for loop but something to that effect. This will create an array with a second dimension which will grow as we add data.

Again, any suggestions are welcome!!
0
 
LVL 2

Expert Comment

by:allie
ID: 7025038
Ummm... and that doesn't work?  Because I do something in a page that's really similar.  Mine looks like this:

var myArray = new Array();
for( var i = 0; i < 3; i++ ){
   var myRecord = new Array();
   myRecord[0] = "Some information";
   myArray[i] = myRecord;
}

That's psudo-code of course - I was doing stuff with recordsets, so myRecord had multiple records, and the "3" was actually an EOF, but this is the gist of it.  I think the major difference between what you posted and this is that I declare the second array, then assign it to the first array, rather than doing both at once.

allie
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:allie
ID: 7025078
Oh wait... I think maybe I misunderstood your question.  All you want is an array that you can append data to at any time?  As opposed to a C array where you define the amount of memory when you declare it and can never change it?  In that case, don't worry about it.  Array() is dynamic by nature.  You don't have to declare a size for it - ever.  Just go ahead and keep adding new arrays.  However, this does require some responsibility on your part - you've got to make sure that you add stuff to the end instead of overwritting existing info (you can use .length to do that).  So whenever you've got a new array you want to add to the main array, you'd have something like this:

var myArray = new Array();
//...some code
var iSize = myArray.length;
myArray[iSize] = new Array();

Does that answer your question?

allie
0
 
LVL 1

Expert Comment

by:vijayneema
ID: 7026486
listening ....
0
 
LVL 2

Accepted Solution

by:
iDeb earned 50 total points
ID: 7028369
almost on the same lines as allie:-

var myArray=new Array();
for(i=0; i<myArray.length; i++)
 myArray[i]=new Array()
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Else condition 9 23
Generate Numbers in JQuery file 11 66
Jquery 2 23
asp Google Map 2 32
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question