0

I can do var foobar = foo.barto access the bar variable.

How would I do this if I had an arbitrary string that told me what element I needed to access. foo."bar" doesn't work.

JAAulde
  • 19,250
  • 5
  • 52
  • 63
Richard Garfield
  • 455
  • 1
  • 5
  • 13
  • Computed is so [], but the way you initially do is also using strings... and every property has a string as name. You can't say "access object property as a string" since it's always string, e.g., the first value at computed index [] casts into string too, so `a[0]` is `a['0']` –  Nov 26 '16 at 01:12
  • if `bar="bat"` then `foo[bar]` would get you `foo.bat` – Jaromanda X Nov 26 '16 at 01:13

1 Answers1

1

I think

foo["bar"]

is the syntax you need

AdamW
  • 186
  • 1
  • 9