How to use FOR-generated number in the name of a variable?

Asked

Viewed 124 times

4

How to use FOR generated number in a variable name ?

Example:

for (var i = 0; i < 5; i++) { 
var teste[i] = "teste é " +[i];
}

Example 2:

for (var b = 0; b < 5; b++) { 
var teste[b] = "teste";
}

OR

I have to create a group of numbered variables, example:

var teste1
var teste2
var teste3...

What better way than one by one ?

  • Create an array. Type: test[i]... No need to save each as a variable.

2 answers

5


Although it is possible, avoid creating variables with dynamic names!

In these examples, you can very well use arrays, where each value can be obtained by the index.

When you say [i], is creating an array with the i in. If I understand the question correctly, you simply want the value i:

var teste = [];
for (var i = 0; i < 5; i++) { 
    teste[i] = "teste é " + i;
}

Notice that I also amended your statement var teste[i], which is a syntax error. Declare the array outside the loop, and within only assign each position. Another alternative is to use push:

var teste = [];
for (var i = 0; i < 5; i++) { 
    teste.push("teste é " + i);
}

3

To bfavaretto response is the most recommended, but in case you need include the numeric index in the variable name for some reason, you can use any object (including the global object window) and dynamically assign it new properties:

for (var i = 0; i < 5; i++) { 
    window["teste" + i] = "teste é " + i;
}
alert(teste3); // "teste é 3"

var testes = {};
for (var b = 0; b < 5; b++) { 
    testes["teste" + b] = "teste";
}
alert(testes.teste3); // "teste"

(where the window, it becomes a global; in the other case, you need to prefix it with the created object; there is no way to create a local variable that way)

Note that this is not a good practice, among other things because you cannot iterate over your easily created variables (as it would be if you used an array). I’m just responding literally to what was asked.

  • 2

    There is even a way to create a local variable (with eval), but I’m glad you didn’t explain :)

  • 1

    @bfavaretto Hey, it’s true! Do I update my answer? : P (seriously, I am in doubt if I keep this answer or delete it - technically it is correct, but it is 110% useless... I can’t think of a single use case for it, and I’ve already abused enough of all language in meta-programming tasks.) By the way, interesting that someone can create local variables dynamically, but there is no way to list all local variables...

  • 1

    My answer got a little weird after the question was edited, because I was answering something else... Your answer is what was asked. At least one complements the other.

  • About listing local variables, there could very well be something "magical" like arguments with the list of properties (including the "inherited" via closures), only that no one has ever implemented. I believe that implementing nay violates the specification. It would be interesting, but imagine how they would abuse it. It would be the new eval.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.