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.
-
1Why do you want a series of variables with those names? What are you trying to accomplish 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– 15ee8f99-57ff-4f92-890c-b561532013年05月30日 14:41:07 +00:00Commented May 30, 2013 at 14:41
-
1What 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– 15ee8f99-57ff-4f92-890c-b561532013年05月30日 14:44:48 +00:00Commented May 30, 2013 at 14:44
-
1An 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– 15ee8f99-57ff-4f92-890c-b561532013年05月30日 14:49:15 +00:00Commented May 30, 2013 at 14:49
-
1Don'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– 15ee8f99-57ff-4f92-890c-b561532013年05月30日 14:51:39 +00:00Commented May 30, 2013 at 14:51
-
1I 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– Dimitri Vorontzov2013年05月30日 15:21:34 +00:00Commented May 30, 2013 at 15:21
8 Answers 8
If 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!
6 Comments
my_array with window inside the for loop, to create global variables of the respective name.window object for a function scope. You could maybe bind your variables to this.this - this[count], or in some other fashion?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]);
}
Comments
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.
Comments
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().
Comments
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]);
}
Comments
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]);
};
Comments
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);
1 Comment
You should use an array.
var myarray = new Array();
myarray[0] = "1";
myarray[1] = "2";
myarray[2] = "3";