Thanks @dfsq for recalling the use of eval .
Here is what I expected, an easy way to evaluate the attribute of an object string.
var obj = { key1 : {key2 : "value1", key3 : "value2"}}; var attr_string = "key1.key2"; var result = eval("obj."+attr_string);
There is no need to split the string into "." and then put it in a loop to get the value. eval can evaluate any string using javascript code instructions.
source share