0

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.

13
  • 1
    Why 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. 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. 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. 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. 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. Commented May 30, 2013 at 15:21

8 Answers 8

5

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!

Sign up to request clarification or add additional context in comments.

6 Comments

That would be the obvious solution, but that's not what I want, for a number of reasons. I actually want to include the number in the name of the variablem rather than use my_array[count].
@Figaro If you want to create variables with "dynamic names" you need some pointer to the scope, where you want to add them. If this is the global scope, e.g., you could replace my_array with window inside the for loop, to create global variables of the respective name.
Sounds like a step in the right direction, @Sirko. That's exactly what I want to do, the variables with "dynamic names", but I think I want them local in scope – how can I do that?
@Figaro a starting point might be the answers in this question, but I know of no equivalent of the window object for a function scope. You could maybe bind your variables to this.
Tried window[count], @Sirko, it does what I want it to do, but it slows down loading of the page considerably. How would you bind variables to this - this[count], or in some other fashion?
|
2

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

2

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

1

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

0

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

Comments

0

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

0
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.net/pe97W/4/

1 Comment

downvote? for what? OP says "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." also says "but my question remains: how do I use the count variable as part of the name of a different variable inside a loop?" --i answered his question, first. And that's downvoting?
0

You should use an array.

var myarray = new Array();
myarray[0] = "1";
myarray[1] = "2";
myarray[2] = "3";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.