• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3362
  • Last Modified:

Create a dynamic two dimensional array in Javascript


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!!!

1 Solution
Take a look at

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

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!!
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.

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

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?

listening ....
almost on the same lines as allie:-

var myArray=new Array();
for(i=0; i<myArray.length; i++)
 myArray[i]=new Array()
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now