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.

2
  • 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'] Commented Nov 26, 2016 at 1:12
  • if bar="bat" then foo[bar] would get you foo.bat Commented Nov 26, 2016 at 1:13

1 Answer 1

1

I think

foo["bar"]

is the syntax you need

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.