javascript 在javascript中将字符串转换为变量名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17175083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a string to a variable name in javascript?
提问by javArc
Ok you have a set of variables:
好的,你有一组变量:
var height1 = 10
var height2 = 20
...
var height7 = 70;
The user has given input and you need to get the value of one of those variables, here's the code for that:
用户已提供输入,您需要获取这些变量之一的值,这是该代码:
if(window.location.hash) {
var hash = window.location.hash.substring(1);
var option_name = $('a[name=' + hash + ']').closest('[id^="option"]').attr("id");
var hash_div_height_id = "height" + option_name.substring(6);
$('a[name=' + hash + ']').closest('[id^="option"]').show();
$('a[name=' + hash + ']').closest('[id^="option"]').height(hash_div_height_id);
} else {
// No hash found
}
My problem now being that I can't pass "hash_div_height_id"(which would currently represent the string "height1" or "height2" or whatever number the jQuery returned to append to the end of the string) to the height function as it's a string.
我现在的问题是我无法将“hash_div_height_id”(当前代表字符串“height1”或“height2”或jQuery返回的任何数字附加到字符串的末尾)传递给height函数,因为它是一个字符串.
So how can I go about this? Can I somehow convert the string "height1" to represent the variable height1 that was established earlier?
那么我该怎么做呢?我可以以某种方式将字符串“height1”转换为表示之前建立的变量 height1 吗?
回答by Sushanth --
Assign the height's as properties of an object.
将高度分配为对象的属性。
Something like..
就像是..
var heightObj = {
height1 : 10,
height2 : 20,
height7 : 70
};
var hash_div_height_id = "height" + option_name.substring(6);
To access the particular property , use the []notation to access the property
要访问特定属性,请使用[]表示法访问该属性
var newHeight = heightObj[hash_div_height_id];
$('a[name=' + hash + ']').closest('[id^="option"]').height(newHeight);
回答by Rune FS
Assuming that the variables are declared in global scope you can just do window[hash]
假设变量是在全局范围内声明的,你可以这样做 window[hash]
That because any variable you declare in global scope is attached as a prpoerty to window
(assuming the code is run in a browser). and you can get the value of any property either by dot notation
那是因为您在全局范围内声明的任何变量都作为属性附加到window
(假设代码在浏览器中运行)。并且您可以通过点表示法获取任何属性的值
window.height1
窗口.height1
or by indexing using the property name as the key. That is
或者通过使用属性名称作为键进行索引。那是
window["height1"]
and since you can pass a variable holding the key you can simply do
并且由于您可以传递保存密钥的变量,因此您可以简单地执行
var hash = "height1"; //replace with the code you already have
var height = window[hash];
回答by Mocksy
Do you have control over the variables as an array of heights would suit you much better, failing that, create the array yourself so you take height 1..X push them onto an array and then you can just use the index.
您是否可以控制变量,因为高度数组更适合您,否则,请自己创建数组,以便将高度 1..X 推到数组上,然后您就可以使用索引了。