This may not be possible, if it is, it does not make up for the effort, the use of an array should be more practical.
var camadas = {};
for (var i = 0; i <= 20; ++i) {
camadas["camada" + i] = 'Hello World ' + i;
}
To retrieve a value, simply specify the key camadaN
, where N is the number in a range between 0 and 20 :
alert(camadas.camada5); // Hello World 5
//alert(camadas["camada5"]); // Sintaxe alternativa
To change the value of a key, do the following:
camadas.camada5 = "foo";
camadas.camada6 = "bar";
// Sintaxe alternativa
//camadas["camada5"] = "foo";
//camadas["camada6"] = "bar";
Exemplo
Update
According to this comment:
I am creating a web page, to caucular tensions on the ground, each
Soil layer has Height and Weight . I created an input that the
person types the number of layers. If the user types 5 for
example, 10 inputs will be generated, 5 for height and 5 for
Specific ... So for each layer I have to generate the variables
to do the calculations .... That's why I need to generate the
each layer ... I created them as objects, in the case a new layer I
I would only instantiate it, but I still have the same problem.
Use an array of arrays . Assuming it is necessary that each layer has specific height and weight, just do the following:
var camadas = {};
for (var i = 0; i <= 20; ++i) {
camadas["camada" + i] = { 'altura': 'altura' + i,
'peso': 'peso' + i};
}
To retrieve the weight and height of a layer, simply specify the camadaN
and the keys, in this case, altura
and peso
. Here's an example:
alert(camadas.camada1.altura); // Valor da altura da camada1
alert(camadas.camada20.peso); // Valor do peso da camada20
Exemplo