Let's say that be have two variables
var variable = "paypay";
var value = "this is the value of paypay!";
With the info above, is there a way to make paypay = "this is the value of paypay!"
?
Let's say that be have two variables
var variable = "paypay";
var value = "this is the value of paypay!";
With the info above, is there a way to make paypay = "this is the value of paypay!"
?
You can put it in a dictionary:
var obj = {};
obj[variable] = value;
Then obj.paypay
is a variable which is now "this is the value of payday!"
.
var variable = "paypay";
var value = "this is the value of paypay!";
Some possibilities
var parent = {};
parent.variable = value; // this is the value of paypay!
parent[variable] = value; // this is the value of paypay!
parent["paypay"] = value; // this is the value of paypay!
parent.paypay = value; // this is the value of paypay!
var "paypay" = value; // error Unexpected string
var paypay = value; // this is the value of paypay!
Assuming that you are properly scoped into a closure or otherwise... then you can cheat and kinda use the "this";
Example:
this[variable] = value;
console.log(variable);
eval("var " + variable + "=" + value);
that is a solution even if using eval() function is not always recommended.