I'm working on a little library that lets me do some basic key value coding w/ JSON objects. Say I have the following JSON array:
var data = { key1: "value1", key2: { nested1: 1, nested2: "wowza!" } };
And I have the following javascript function:
var setData = function(path, value) {
eval("data." + path + "= value;");
};
And in use:
setData("key1", "updated value1"); // data.key1 == "updated value1"
setData("key2.nested1", 99); // data.key2.nested1 == 99
This works, however I would like to accomplish the above w/o using eval. Is this possible, or is eval the best way to go?
EDIT:
NOTE it can be assumed the value you're setting exists, at least for path depth - 1. I'm more concerned about setting the value of an existing JSON object.