0

I have an object that stores 3 variables called text0, text1 and text2 that in turn store one string each. I also have a variable that stores the index of the clicked button, so that if I click on the third button it should fetch the text2.

I was trying something like this:

p.text(myObj.text + btnNumber);

But so far all I get is NaN. Is it even possible to do this way? What am I missing?

Wanna Coffee
  • 2,612
  • 7
  • 38
  • 63
Spner
  • 15
  • 1

1 Answers1

4

Use named indexing of the object to get a variable named property:

p.text(myObj["text" + btnNumber]);

Javascript lets you index properties of an object as if it were a dictionary (indexed via string names).

Gone Coding
  • 90,552
  • 24
  • 176
  • 195