如何在 JavaScript 对象中插入一个变量作为键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5640988/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:12:57  来源:igfitidea点击:

How do I interpolate a variable as a key in a JavaScript object?

javascriptdictionary

提问by Geo

How can I use the value of the variable aas a key to lookup a property? I want to be able to say: b["whatever"]and have this return 20:

如何使用变量的值a作为查找属性的键?我希望能够说:b["whatever"]并有这个回报 20:

var a = "whatever";
var b = {a : 20};     // Want this to assign b.whatever
alert(b["whatever"]); // so that this shows 20, not `undefined`

I am asking if it's possible during the creation of b, to have it contain "whatever":20instead of a:20where "whatever" is itself in a variable. Maybe an evalcould be used?

我在问在创建 , 的过程中是否有可能b让它包含"whatever":20而不是a:20“任何”本身在变量中的位置。也许eval可以使用?

回答by mVChr

var a = "whatever";
var b = {};
b[a] = 20;
alert(b["whatever"]); // shows 20

回答by ucstre

This works in Firefox 39 and Chrome 44. Don't know about other browsers. Also it doesn't work in nodejs v0.12.7.

这适用于 Firefox 39 和 Chrome 44。不知道其他浏览器。它也不适用于 nodejs v0.12.7。

var a = "whatever";
var b = { [a]: 20 };
console.log(b["whatever"]); // shows 20

That is, to interpolate a variable, enclose it in brackets.

也就是说,要插入一个变量,请将其括在括号中。

I'm not sure if this is a part of any standard. Originally, I saw such syntax here: https://hacks.mozilla.org/2015/07/es6-in-depth-classes/where the author defined:

我不确定这是否是任何标准的一部分。最初,我在这里看到了这样的语法:https: //hacks.mozilla.org/2015/07/es6-in-depth-classes/,作者定义的地方:

[functionThatReturnsPropertyName()] (args) { ... }

I'm also not sure if you should use that syntax. It's not widely known. Other members on your team might not understand the code.

我也不确定您是否应该使用该语法。它并不广为人知。您团队中的其他成员可能不理解代码。

回答by Rocket Hazmat

var a = "whatever";
var b = {a : 20};
b[a] = 37;
alert(b["whatever"]); // 37

'a'is a string with the value 'a'. ais a variable with the value 'whatever'.

'a'是一个值为 的字符串'a'a是一个值为 的变量'whatever'

回答by rashadb

Great question. I had a time trying to figure this out with underscore but the answer couldn't be more simple:

很好的问题。我有一段时间试图用下划线解决这个问题,但答案再简单不过了:

var a = "whatever";
var b = {a : 20};

var array = [a, b]
var answer = _.object([[array]])// {whatever: {a:20}}//NOTICE THE DOUBLE SET OF BRACKETS AROUND [[array]]

I hope this helps!

我希望这有帮助!

回答by Naftali aka Neal

Try this:

尝试这个:

var a = "whatever";
var c = "something";
var b = {whatever : 20, something: 37};
alert(b[a]); // Shows 20
alert(b[c]); // Shows 37

Here is the fiddle.

这是小提琴

Or if I understand from the below comments correctly, try this:

或者,如果我从以下评论中正确理解,请尝试以下操作:

var a = "whatever";
var b = {a : 20};
alert(b.a); // Shows 20

回答by Benny Neugebauer

To show all options, I want mention the CoffeeScriptway of doing this, which is:

为了显示所有选项,我想提及执行此操作的CoffeeScript方式,即:

var a = "whatever";
var b = (
  obj = {},
  obj["" + a] = 20,
  obj
);

alert(b.whatever); // 20

Although I prefer:

虽然我更喜欢:

var a = "whatever";
var b = {};
b[a] = 20;

alert(b.whatever); // 20