Create a dynamic two dimensional array in Javascript

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.
NoelleBlaneyAsked:
Who is Participating?
 
iDebConnect With a Mentor Commented:
almost on the same lines as allie:-

var myArray=new Array();
for(i=0; i<myArray.length; i++)
 myArray[i]=new Array()
0
 
estraussCommented:
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
 
NoelleBlaneyAuthor Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
allieCommented:
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
 
allieCommented:
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
 
vijayneemaCommented:
listening ....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.