?
Solved

Create a dynamic two dimensional array in Javascript

Posted on 2002-05-21
6
Medium Priority
?
3,339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

650 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