That maybe it is not possible, if it is, not worth the effort, the use of a array should be more practical.
var camadas = {};
for (var i = 0; i <= 20; ++i) {
camadas["camada" + i] = 'Hello World ' + i;
}
To recover a value, just 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:
camadas.camada5 = "foo";
camadas.camada6 = "bar";
// Sintaxe alternativa
//camadas["camada5"] = "foo";
//camadas["camada6"] = "bar";
Exemplo
Updating
As per this comment:
I’m creating a web page, to caucular the tensions in the ground, each
soil layer has Height and Heaviness Specific. I created a input that the
person type the amount of layers. If user type 5 per
example, 10 inputs will be generated, 5 for height and 5 for weight
Specific... So for each layer I have to generate the variables
to make the calculations.... That’s why I need to generate the variables of
each layer...I created them as objects, in this case a new layer I
I would only instantiate her, but I still have the same problem.
Use a 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 regain the weight and height of a layer, simply specify the camadaN
and the keys in case, altura
and peso
. Take an example:
alert(camadas.camada1.altura); // Valor da altura da camada1
alert(camadas.camada20.peso); // Valor do peso da camada20
Exemplo
If these variables are global, until it is possible: just use
window["camada" + (i+1)] = 12;
(attention to the association of the operator+
). Then you could access it, say, likex = camada4;
. But as already said in the answers, this is a bad idea, even if they were not global, better to use an array even or maybe an object.– mgibsonbr
Can you explain what the fineness is where you need it? If you mention the final problem we can give the right solution.
– Sergio
I am creating a web page, to caucular the tensions in the soil, each layer of soil has Specific Height and Weight I created an input that the person type the amount of layers. If the user type 5 for example, 10 inputs will be generated, 5 for height and 5 for Specific Weight... So for each layer I have to generate the variables to make the calculations.... That’s why I need to generate the variables of each layer...I created them as objects, in case a new layer I would only leave it, but I still have the same problem
– Jacob Athias
I want to make use of the following form --for (var i=0; i<20; i++) { Document.getElementById("thickening"+(i+1)). innerHTML=" "+layer(i+1). height +"m"
– Jacob Athias
I’ll test...Thanks already bring the answer
– Jacob Athias
Friend, you got it right, that’s what I wanted...many thanks var layers = {}; for (var i = 0; i <= 20; ++i) { layers["layer" + i] = { 'height': 'height' + i, 'weight': 'weight' + i}; }
– Jacob Athias
@Great Jacobathias. If possible mark the reply as the one that solved, click on the below of the score. Also vote positively on the answers that helped you!
– stderr