How can I use the value of the variable a
as a key to lookup a property? I want to be able to say: b["whatever"]
and have this return 20:
如何使用变量a的值作为查找属性的键?我希望能够说:b [“无论如何”]并获得此回报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":20
instead of a:20
where "whatever" is itself in a variable. Maybe an eval
could be used?
我在询问是否有可能在创建b期间让它包含“无论”:20而不是:20其中“what”本身就是变量。也许可以使用eval?
7 个解决方案
#1
42
var a = "whatever";
var b = {};
b[a] = 20;
alert(b["whatever"]); // shows 20
#2
32
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.
我也不确定你是否应该使用这种语法。它并不广为人知。您团队中的其他成员可能无法理解代码。
#3
5
var a = "whatever";
var b = {a : 20};
b[a] = 37;
alert(b["whatever"]); // 37
'a'
is a string with the value 'a'
. a
is a variable with the value 'whatever'
.
'a'是一个值为'a'的字符串。 a是值为'whatever'的变量。
#4
5
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!
我希望这有帮助!
#5
2
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
#6
0
To show all options, I want mention the CoffeeScript way 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
#7
0
What if it is
如果它是什么
var a = "whatever.other";
var b = {a:20};
expected output b["whatever"]["other"] should return 20
#1
42
var a = "whatever";
var b = {};
b[a] = 20;
alert(b["whatever"]); // shows 20
#2
32
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.
我也不确定你是否应该使用这种语法。它并不广为人知。您团队中的其他成员可能无法理解代码。
#3
5
var a = "whatever";
var b = {a : 20};
b[a] = 37;
alert(b["whatever"]); // 37
'a'
is a string with the value 'a'
. a
is a variable with the value 'whatever'
.
'a'是一个值为'a'的字符串。 a是值为'whatever'的变量。
#4
5
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!
我希望这有帮助!
#5
2
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
#6
0
To show all options, I want mention the CoffeeScript way 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
#7
0
What if it is
如果它是什么
var a = "whatever.other";
var b = {a:20};
expected output b["whatever"]["other"] should return 20