问题
I need to dynamically create variables inside a loop, but i have no idea how. I have read about using eval(); but all i found it's just about dynamically changing the value inside the variable.
I need something like this:
$(e).find('unidadeid').each(function () {
countUnidades++;
var Unidade[value inside countUnidades here] = $(this).text();
});
Be clear about your steps, please. I ask not for a solution, but for a help, as a learning. Thank you ;)
回答1:
You have two options:
Use an array:
var unidades = []; $(e).find('unidadeid').each(function () { unidades.push($(this).text()); });or
var unidades = $(e).find('unidadeid').map(function () { return $(this).text(); }).get();If you really, really want to have names that aren't just digits, use an object and bracketed notation:
var unidades = {}, countUnidades = 0; $(e).find('unidadeid').each(function () { countUnidades++ unidades['Unidade' + countUnidades] = $(this).text(); });That creates an object,
unidades, with properties likeUnidade0,Unidade1, etc. Note thateachreceives an index, so you don't needcountUnidadesunless you want it for something else:var unidades = {}; $(e).find('unidadeid').each(function (index) { unidades['Unidade' + index] = $(this).text(); });
回答2:
You can always use array:
var Unidade = [],
countUnidades = 0;
$(e).find("unidadeid").each(function(i) {
Unidade[countUnidades++] = $(this).text();
// ... or Unidade.push($(this).text());
// ... or Unidade[i] = $(this).text();
});
Don't forget that DOM elements should always have unique IDs, so each iteration is not required here, if unidadeid refers to ID of some element.
回答3:
Your example is simply adding to what I assume is an array with incremental values. Unidade would look something like this in JSON:
[
0: "text0",
1: "text1",
2: "text2"
]
But if you wanted to add this to an object, you would just iterate through an array of strings where the array is of the same length as your list of items. In fact you might want to attach this value to a data attribute where the value is the variable name and the text is the content of the element. So the HTML would be something like this:
<div data-var="a">Text1</div>
<div data-var="b">Text2</div>
<div data-var="c">Text3</div>
Would require this JS:
var unidade = {};
$("div").find('unidadeid').each(function() {
unidade[$(this).data('var')] = $(this).text();
});
console.log(unidade);
And the object would look like this:
{
"a": "Text1",
"b": "Text2",
"c": "Text3"
}
回答4:
I doubt you need to create variables dynamically. That's very rarely helpful.
You probably want to learn about data structures; in particular I think you just want an array:
var unidades = []; // declare the array outside the loop
$(e).find('unidadeid').each(function () {
countUnidades++;
unidades[countUnidades] = $(this).text();
});
And now you have an array, unidades whose values are the text() contents of your elements.
I'm not sure if find('unidadeid') is what you want - that's finding all <unidadeid> elements, which is not an HTML element, but maybe you're processing custom XML, or maybe it was just an example.
回答5:
Well, just for completeness, here is exactly what you asked for, eventhough it's a terrible idea:
$(e).find('unidadeid').each(function () {
countUnidades++;
var t = $(this).text();
eval('Unidade' + countUnidades + ' = t;');
});
Note:
evalis hard to use without opening up security holes.evalis slow.- Creating variables dynamically is rather pointless because you need to access them dynamically also.
- The variables are global (as otherwise they would be local to the callback function and go away right after being created).
Normally you would use an array instead:
var Unidade = [];
$(e).find('unidadeid').each(function () {
Unidade.push($(this).text());
});
来源:https://stackoverflow.com/questions/15786523/dynamically-name-variables-javascript