0

Is it possible to give a user an option which string he can use?

I have an object: myObject which has 3 string

myObject.string1
myObject.string2
myObject.string3

I'm trying to do something like this:

var whichString = "string1";

myObject.whichString 
myObject.(whichString)
myObject.[whichString]

None of the above works. Is there a way to do it, please?

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
PAyTEK
  • 57
  • 7
  • I assumed your two last lines are the result of lazy copy-pasting but should read `myObject.(whichString)` and `myObject[whichString]` , right ? – Romain Valeri Jul 22 '18 at 09:59
  • Hey, @RomainVALERI you don't need to judge. I'm a beginner user. If you want to help. Just help. Thanks – PAyTEK Jul 22 '18 at 10:10
  • I'm very sorry, I never wanted to shame you by using the term *lazy*, we're ALL guilty of this once in a while, and I clumsily wanted to refer to it as a minor typing mistake, not a reasoning mistake. – Romain Valeri Jul 22 '18 at 10:13
  • Ok, no problem & thanks for the help. It's working now :-) – PAyTEK Jul 22 '18 at 10:17

1 Answers1

2

If your myObject object is created like this :

var myObject = {
  string1: "abc",
  string2: "def",
  string3: "xyz"
}

Then

var whichString = "string1";

alert(myObject[whichString]);// this will alert "abc"

should be fine.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61