[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Declaring multiple variables on the fly in javascript

Posted on 2007-09-28
5
Medium Priority
?
597 Views
Last Modified: 2007-09-28
How can I declare variable on the fly/in a loop? I need to declare a number of arrays. This number will change based on the number of records I return from a query. It'll be anyware from 50-200 or more. If I do it statically, it would look like this:

          var DynamicArray1 = new Array(null,null,null,null,null,null,null,null);
          var DynamicArray2 = new Array(null,null,null,null,null,null,null,null);
          var DynamicArray3 = new Array(null,null,null,null,null,null,null,null);
          var DynamicArray4 = new Array(null,null,null,null,null,null,null,null);
          var DynamicArray5 = new Array(null,null,null,null,null,null,null,null);
          var DynamicArray6 = new Array(null,null,null,null,null,null,null,null);

But I want to do it something like this:

var loopcount = 6
for (var i=1;i<=loopcount ;i++){
          var DynamicArray+i = new Array(null,null,null,null,null,null,null,null);
}

But it doesn't seem to be working. Any help?

Thanks in advance.
0
Comment
Question by:niner
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Morcalavin
ID: 19979236
Why not make DynamicArray an array itself, with it's index containing the arrays you want.

var loopcount = 6
var DynamicArray = new Array();

for (var i=1;i<=loopcount ;i++){
          DynamicArray[i] = new Array(null,null,null,null,null,null,null,null);
}

so DynamicArray[2] would contain an array.
0
 

Author Comment

by:niner
ID: 19979651
But will that declare a variable named DynamicArray2 so that I can reference it as DynamicArray2 later in the code?
0
 
LVL 18

Accepted Solution

by:
Morcalavin earned 500 total points
ID: 19979743
No.  The name would be DynamicArray[2].  You can create dynamicaly-named variables by using:
window["DynamicArray" + i]

But I think it's use is discouraged.  Someone else would have to confirm that though.
0
 

Author Comment

by:niner
ID: 19979865
So it would be:

var loopcount = 6
var DynamicArray = new Array();

for (var i=1;i<=loopcount ;i++){
window["DynamicArray" + i] = new Array(null,null,null,null,null,null,null,null);
}
?
0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 19979934
Yes, but you wouldn't need this line "var DynamicArray = new Array();"
0

Featured Post

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.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…
Suggested Courses

834 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