I'm looking for suggestions on how to select properties on the child level or deeper dynamically.
For example, here is the method that I am currently using. The method parameters is the object
and either a string
or array
for the property name. If a string
is passed used, it will just will get the property. The array is iterated through in order to select the property value.
getMappedDataProperty: function(obj, map) {
var m, result, _i, _len;
if (typeof map === 'string') {
return obj[map];
}
result = obj;
for (_i = 0, _len = map.length; _i < _len; _i++) {
m = map[_i];
result = result[m];
}
return result;
}
Even though this works, I keep thinking to myself that there has to be a better way of accomplishing this. Suggestions?
'foo.bar[0]' => ['foo', 'bar', '0']
\$\endgroup\$