0

How can I convert a string to a variable name that I can use to access a value in a json object?

This works, but "fname" is hard coded, not dynamic:

$("#ppl").append(people[i].fname);

I want something like this:

var myString = "fname";

$("#ppl").append(people[i].myString); // doesn't work

or

$("#ppl").append(people[i].window['myString']); // also doesn't work

Help!

Here is the jsfiddle

3
  • 2
    people[i][myString] should work? Commented Oct 14, 2014 at 22:39
  • you rock. worked! thanks. Commented Oct 14, 2014 at 22:40
  • Note: That's not JSON, that's a Javascript object. JSON is a text format to represent objects. Commented Oct 14, 2014 at 23:07

1 Answer 1

1

people[i][myString]

Will work.

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.