You can use eval , but here is the best solution:
// The following code results in the same as that scary eval(...) var data = binding.data, chain = binding.field.split(/[\.\[\]]+/); for (var i = 0; data[chain[i]]; i++) { data = data[chain[i]]; } // Embrace JavaScript Awesomeness!
A breakdown of what I'm doing here:
In JS, any property can be called object[propertyName] .
This includes arrays, i.e. a[3] same as a['3'] .
Therefore, we will split the string into one of the characters:. , [ , ] . + exists, because without it, if you have a[3].b[3] , ]. will give you an empty string.
We can get an empty string at the end, but this is not a problem, since "" is like false in JS.
You can go ahead and filter out all invalid variable names that in javascript is a name that does not match [a-zA-Z_$][0-9a-zA-Z_$]* . But I'm not quite sure why this can be done ...
glebm source share