I suspect I am making a mistake in a basic JavaScript syntax.
var my_array = new Array(10);
for (var count=0; count<my_array.length; count++) {
var my_array+count = "This is a variable number "+count+".";
document.write(my_array+count);
}
I want the loop to create series of variables that should be called my_array0, my_array1, my_array2, and so on. The code above is how I tried to do that, but it doesn't work. What's the correct way of naming the variable inside the loop?
EDIT: I know I could use my_array[count], but that's not what I'm looking for. What I need is to be able to name a variable inside the loop, using the index as part of the name of the variable.
I suspect I am making a mistake in a basic JavaScript syntax.
var my_array = new Array(10);
for (var count=0; count<my_array.length; count++) {
var my_array+count = "This is a variable number "+count+".";
document.write(my_array+count);
}
I want the loop to create series of variables that should be called my_array0, my_array1, my_array2, and so on. The code above is how I tried to do that, but it doesn't work. What's the correct way of naming the variable inside the loop?
EDIT: I know I could use my_array[count], but that's not what I'm looking for. What I need is to be able to name a variable inside the loop, using the index as part of the name of the variable.
Share Improve this question edited May 30, 2013 at 14:41 Dimitri Vorontzov asked May 30, 2013 at 14:36 Dimitri VorontzovDimitri Vorontzov 8,11412 gold badges51 silver badges78 bronze badges 13- 1 Why do you want a series of variables with those names? What are you trying to acplish here? If you want ten arrays, create ten arrays and put them in another array. Do you know what an array is? It is a thing that holds a list of multiple other things. – 15ee8f99-57ff-4f92-890c-b56153 Commented May 30, 2013 at 14:41
- 1 What are you trying to DO? WHY do you want a series of variables with different names? If you want ten things, put them in an array. If you want to put something else in a different array, create a second array. – 15ee8f99-57ff-4f92-890c-b56153 Commented May 30, 2013 at 14:44
- 1 An image isn't an array. Why are you putting an image in a variable named "my_array0"? You want two arrays: var imagesrc = new Array(10); var images = new Array(10); Populate imagesrc[0] through imagesrc[9] with the urls, then populate images with the Images, then assign the .src properties. – 15ee8f99-57ff-4f92-890c-b56153 Commented May 30, 2013 at 14:49
- 1 Don't do that. That's what arrays are for. They are much easier to use. It's cool to try to think of your own new ways to solve problems, but this one isn't a great idea. It's hard enough to learn programming without trying to do things the language doesn't want you to do. – 15ee8f99-57ff-4f92-890c-b56153 Commented May 30, 2013 at 14:51
- 1 I experimented with various possible solutions, and your suggestion of using two arrays, @Ed, is the best one. I had to accept the answer that most directly addresses my original question, but you made me rethink the way I approached the problem, and I very much appreciate it. Your suggestion was the solution. – Dimitri Vorontzov Commented May 30, 2013 at 15:21
8 Answers
Reset to default 5If you want to set the elements of an array, use the []
syntax:
var my_array = new Array(10);
for (var count=0; count<my_array.length; count++) {
my_array[count] = "This is a variable number "+count+".";
document.write( my_array[count] );
}
Furthermore, when specifying just an element of an array and not the array itself, drop the var
in front of it!
This pattern is questionable, and the array seems unnecessary; however, here is one way to do it:
var my_array = new Array(10);
for (var count = 0; count < my_array.length; count++) {
window['my_array' + count] = "This is a variable number " + count + ".";
document.write(window['my_array' + count]);
}
What's the correct way of naming the variable inside the loop? You don't.
If you just want a variable to hold that particular value, just use an ordinary variable. If you want lots of different values, stick it inside an array or object. But that's redundant here since you already have an array, so I'm really not sure what you're trying to achieve.
And if none of the previous answers suits you, you can always use eval()
var varName = 'my_array'
for (var count=0; count<my_array.length; count++) {
eval(varName+count +" = This is a variable number "+count+".");
}
Edit: @Noah Freitas provides a better answer, without using eval()
.
You're redefining my_array inside the loop, and not accessing the variable correctly either. Try:
var my_array = new Array(10);
for (var count=0; count<my_array.length; count++) {
my_array[count] = "This is a variable number "+count+".";
console.log(my_array[count]);
}
JS Fiddle
You can't execute on the left-hand side of the assignment operator (=
), you can only assign. Execution, in javascript, takes place on the right hand side.
var my_array = new Array(10);
var var_hashmap = {}; // create a new object to hold our variables.
for (var count = 0; count < my_array.length; count++) {
var key = "my_array" + count;
var value = "This is a variable number " + count + ".";
var_hashmap[key] = value;
document.write(var_hashmap[key]);
};
var my_array = new Array(10);
for (var count=0; count<my_array.length; count++)
{
eval("var my_array" + count + " = 'This is a variable number'+count+' and the variable name is my_array'+count");
}
alert(my_array0);
alert(my_array1);
alert(my_array2);
alert(my_array3);
alert(my_array4);
alert(my_array5);
alert(my_array6);
alert(my_array7);
alert(my_array8);
alert(my_array9);
http://jsfiddle/pe97W/4/
You should use an array.
var myarray = new Array();
myarray[0] = "1";
myarray[1] = "2";
myarray[2] = "3";