Dynamically Forming a JSON object traversion without using eval.

Posted by Matt Willhite on Stack Overflow See other posts from Stack Overflow or by Matt Willhite
Published on 2010-03-26T20:20:14Z Indexed on 2010/03/26 20:23 UTC
Read the original article Hit count: 328

Filed under:
|
|

Given I have the following: (which is dynamically generated and varies in length)

associations = ["employer", "address"];

Trying to traverse the JSON object, and wanting to form something like the following:

data.employer.address

or

data[associations[0]][association[1]]

Without doing this:

eval("data."+associations.join('.'));

Finally, I may be shunned for saying this, but is it okay to use eval in an instance like this? Just retrieving data.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about eval