admin管理员组文章数量:1277400
I'm having some difficulty creating a variable with another variable in jQuery. I don't know how to write the var side of the equation. Here's what I'm trying to create:
var $counter= 0;
$('.selector').each(function(){
$counter += 1;
var newVariable-($counter) // this is where I'd like to create the new
// variable with the number from $counter at the end.
});
with the goal to creating:
newVariable-1
newVariable-2
newVariable-3...
and so on...
I'm having some difficulty creating a variable with another variable in jQuery. I don't know how to write the var side of the equation. Here's what I'm trying to create:
var $counter= 0;
$('.selector').each(function(){
$counter += 1;
var newVariable-($counter) // this is where I'd like to create the new
// variable with the number from $counter at the end.
});
with the goal to creating:
newVariable-1
newVariable-2
newVariable-3...
and so on...
Share Improve this question edited Jun 13, 2014 at 4:42 ngrashia 9,9045 gold badges44 silver badges58 bronze badges asked Jun 13, 2014 at 4:35 user3717403user3717403 431 gold badge1 silver badge4 bronze badges 2- 1 stackoverflow./questions/2895930/… – CRABOLO Commented Jun 13, 2014 at 4:38
-
Readability tip: Try to use variables beginning with
$
only for caching jQuery selections. (var $counter = $('#counter'); var counter = 0;
) – christian314159 Commented Jun 13, 2014 at 4:41
3 Answers
Reset to default 8You could create an object to hold these values but not dynamic variables.
var $counter= 0;
var variableHolder = {};
$('.selector').each(function(){
$counter += 1;
variableHolder["newVariable-"+$counter] = ...
});
Or if you want to make global variables (which is not remended), you could use window
:
var $counter= 0;
$('.selector').each(function(){
$counter += 1;
window["newVariable-"+$counter] = ...
});
As others have pointed out, using an {}
with square bracket notation will simplify this task greatly.
Something like this:
var myobj = {},
prefix = 'my_cool_var';
for(var i = 0, len = 10; i < len; i++) {
myobj[prefix + i] = undefined; // { my_cool_var + i : undefined }
}
// Setters - dot notation and square bracket
myobj.my_cool_var1 = "Hello!";
myobj['my_cool_var2'] = "Hello 2!";
// Getters - dot notation and square bracket
alert(myobj.my_cool_var1); // alerts Hello!
alert(myobj['my_cool_var2']); // alerts Hello 2!
Now, if you needed to expose the variables in a global scope (yuck - but hey, sometimes you gotta) so you don't need to specify an object (myobj
), you can use window
with square bracket notation in your for loop
.
var prefix = 'my_global_var';
for(var i = 0, len = 10; i < len; i++) {
window[prefix + i] = undefined; // creates global, my_global_var + i = undefined
}
my_cool_var1 = "Hello!";
alert(my_cool_var1); // alerts Hello!
Finally, if you search the web deep enough, you'll find eval
examples like this:
var prefix = 'my_evil_var';
for(var i = 0, len = 10; i < len; i++) {
// Don't do this. Use square bracket notation with window, if you need a global.
eval(prefix + i + '= undefined') // creates global, my_evil_var + i = undefined
}
my_evil_var = "Eval abuse is bad!!";
alert(my_evil_var1); // alerts Eval abuse is bad!!
Hope this helps!
Just make use of the json in this context,
var $counter= 0;
var $newVar = {};
$('.selector').each(function(){
$counter += 1;
$newVar['newVariable-'+ ($counter)] = null;
});
so you can access it like $newVar.newVariable-1
,.. $newVar.newVariable-N
And please note that this is the best practice, we could do as you asked by accessing the window object, but that is not remended.
本文标签: javascriptCreating a variablewith a variable in an each loopjQueryStack Overflow
版权声明:本文标题:javascript - Creating a variable, with a variable in an each loop - jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741256057a2366693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论