Javascript 如何在散列中将变量值分配为变量名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4199126/
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
how to assign variable value as variable name in a hash?
提问by mikkelbreum
I need to define a hash for posting some ajax data using jQuery. The hash will look something like:
我需要定义一个散列来使用 jQuery 发布一些 ajax 数据。哈希将类似于:
var setname = 'set_1';
elements = { set_1: {'beer','water','wine'} };
The challenge I need to solve is 'set-1' (the key of Array elements) should be dynamically named based on the value of var setname
.
我需要解决的挑战是 'set-1'(数组元素的键)应该根据var setname
.
I want to avoid using eval()
of course..
in PHP it can be done using the double dollar sign like this: $$setname
, but what's the way to do this in JavaScript?
我eval()
当然想避免使用.. 在 PHP 中它可以使用像这样的双美元符号来完成:$$setname
,但是在 JavaScript 中这样做的方法是什么?
回答by Daniel Vandersluis
You can do what you'd like to like so:
你可以做你想做的事:
var setname = 'set_1', elements = {};
elements[setname] = ['beer','water','wine'];
alert(elements['set_1']); // beer,water,wine
See this in action at http://jsfiddle.net/x5KRD/.
在http://jsfiddle.net/x5KRD/ 上查看此操作。
All objects in JS can be accessed using dot notation(obj.method()
or obj.property
), or bracket notation(obj['method']()
or obj['property']
). Using bracket notation lets you dynamically specify method/property/key names.
JS 中的所有对象都可以使用点符号(obj.method()
或obj.property
)或括号符号(obj['method']()
或obj['property']
)访问。使用括号表示法可以动态指定方法/属性/键名称。
For example, while clumsy, window['alert']('hi')
is equivalent to window.alert('hi')
.
例如,虽然笨拙,但window['alert']('hi')
相当于window.alert('hi')
.
Note that your code won't work as-is, anyways, because you're using object literal notation ({'beer','water','wine'}
) to contain an array (it should be in square brackets ['beer','water','wine']
instead). Object literals need to have key-value pairs.
请注意,无论如何,您的代码不会按原样工作,因为您使用对象文字符号 ( {'beer','water','wine'}
) 来包含数组(它应该放在方括号中['beer','water','wine']
)。对象字面量需要有键值对。
回答by kanaka
var setname = 'set_1',
elements = {};
elements[setname] = ['beer','water','wine'];
回答by Pointy
You have to understand that there's really no such thing as "JSON notation" in Javascript - it's native Javascript notation that we're talking about. What jQuery wants is a Javascript value for the POST data, not necessarily a Javascript object constant.
你必须明白在 Javascript 中真的没有“JSON 表示法”这样的东西——我们正在谈论的是原生 Javascript 表示法。jQuery 想要的是 POST 数据的 Javascript 值,不一定是 Javascript 对象常量。
Thus, your code will prepare your POST data like this:
因此,您的代码将像这样准备您的 POST 数据:
var elements = {};
elements[setName] = { /* something */ };
elements[somethingElse] = { /* something else */ };
and so on. When you're ready to do the POST, you'd just use "elements":
等等。当您准备好进行 POST 时,您只需使用“元素”:
$.post(url, elements, function() { /* callback code */ });