Reputation: 118
I want to shorten these lines
var upgradethis = "u3";
if (nev == "u1"){
mM.u1.count.text=minionCounter[lvl];
}
if (nev == "u2"){
mM.u2.count.text=minionCounter[lvl];
}
How can I replace the "u1" and "u2" with value of "upgradethis"?
And use in here:
mM.upgradethis's value.count.text=minionCounter[lvl];
(replace the u2 in this code to value of "upgradethis")
Upvotes: 0
Views: 97
Reputation: 6043
As per Question with no answers, but issue solved in the comments (or extended in chat):
Use mM[upgradethis].count.text
to solve the issue.
Solution attributed to Cherniv, who posted it here.
Note that you can run into a TypeError #1010: A term is undefined and has no properties
if you don't check if the property exists. You can use hasOwnProperty
(syntax if(mm.hasOwnProperty(upgradethis))
) for this.
Upvotes: 3