0
I have this example of code:
var a = 'foo';
var b = {a:'2'};
b.foo //undefined
what I wish is that b.foo exists and returns "2" in this example, but when creating the variable "b" the variable "a" ceases to exist when called inside the object what happens is that the property name becomes "a"... how to make the property name be the value of an existing variable?
This code doesn’t make any sense. Demonstrate better what you want for us to see a viable way to solve what you really need.
– Maniero
Do you want the value of the variable to be the name of the object attribute ? type that ? https://jsfiddle.net/filadown/whx4tctz/
– Gabriel Rodrigues
I think he wants something like:
b['foo'] = '2';
– Franchesco
Here’s a good answer: http://answall.com/a/82668/14674
– Franchesco
Guys, thanks for the help. The doubt was really simple, just use b[a] = '2' =)! I’m taking the rust out of Js little by little here, thank you!!!
– tiagomac