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:
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?
-
possible duplicate of How to create a dynamic key to be added to a javascript object variable. and pass variable into javascript object and probably others...Felix Kling– Felix Kling2011年04月12日 20:25:40 +00:00Commented Apr 12, 2011 at 20:25
-
1I'm guessing when you say "interpolate," you mean "use." Just so it doesn't cause you problems in the future, interpolate means "estimate unknown values between known values" and doesn't fit in this question title at all.foobarbecue– foobarbecue2015年02月03日 15:26:51 +00:00Commented Feb 3, 2015 at 15:26
-
4No, he's using correct terminology. You're referring to interpolation in the field of mathematics. en.wikipedia.org/wiki/Interpolation He's referring to interpolation in the field of computer science. en.wikipedia.org/wiki/String_interpolation. :-)Aaron Gray– Aaron Gray2015年07月23日 23:21:20 +00:00Commented Jul 23, 2015 at 23:21
-
I think stackoverflow.com/questions/6500573/… asks the same thing but formulated in a different way, neither of them in a particularly wrong way.conny– conny2018年04月17日 16:22:30 +00:00Commented Apr 17, 2018 at 16:22
6 Answers 6
This works in Firefox 39 and Chrome 44. Don't know about other browsers. Also it doesn't work in 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:
[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.
5 Comments
var a = "whatever";
var b = {};
b[a] = 20;
alert(b["whatever"]); // shows 20
1 Comment
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'.
Comments
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!
1 Comment
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
7 Comments
b.a is parsed as b['a'] not b['whatever']To show all options, I want mention the CoffeeScript way of doing this, which is:
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