Can you use variables in a variable name in Javascript?
-
var array1 = new Array(4); var array2 = new Array(4); var array3 = new Array(4); var array4 = new Array(4); var array5 = new Array(4); var array6 = new Array(4); var array7 = new Array(4); for(var a = 1; a < 8; a++){ array+ a = new Array(4); } I want to make an array with a for loop, but the variables has to be diffrent every time. So my question if this is possible, and if it is, how?
-
Answer:
No you can't (not unless you use Eval() which you shouldn't...). As Justinas has commented, you could use a Multidimensional array. var array = []; array.push(new Array(4)); array.push(new Array(4)); array.push(new Array(4)); array.push(new Array(4)); array.push(new Array(4)); array.push(new Array(4)); array.push(new Array(4)); for(var a = 1; a < 8; a++){ array[a] = new Array(4); }
Twan Korthout at Stack Overflow Visit the source
Other answers
You can do like so: var myArrays = {}; myArrays["arrA"] = new Array(5); myArrays["anotherArr"] = new Array(8); ...
Galman33
Related Q & A:
- How to retrieve a variable name given its value?Best solution by Stack Overflow
- What can I use to clean a DVD?Best solution by Yahoo! Answers
- Can I use silverdine for a scar?Best solution by Yahoo! Answers
- Can you use "I" in a college admissions essay?Best solution by huffingtonpost.com
- Can anyone help me create a screen name for myself?Best solution by nameideas.wordpress.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.