Path variable is unpredictable. Sometimes it is just a
, sometimes a/b
, sometimes a/b/c
etc. I want to reach a node dynamically according to path. The code below behaves what i want but i can consider if there is a better way to do that without eval
for example.
http://jsfiddle.net/nuonzngv/1/
cont = {
"a" : {
"b": {
"c": "d"
}
}
}
path = "a/b/c";
sect = path.split("/");
path = "cont";
$.each(sect, function( index, value ) {
path = path + "['" + value + "']";
});
console.log(eval(path));
Solution
I found a plugin that has a getPath function in it, for underscore.js: https://github.com/documentcloud/underscore-contrib/blob/master/docs/underscore.object.selectors.js.md