utils.js 350 B

1234567891011
  1. exports.lookup = function(obj, field) {
  2. if (!obj) { return null; }
  3. var chain = field.split(']').join('').split('[');
  4. for (var i = 0, len = chain.length; i < len; i++) {
  5. var prop = obj[chain[i]];
  6. if (typeof(prop) === 'undefined') { return null; }
  7. if (typeof(prop) !== 'object') { return prop; }
  8. obj = prop;
  9. }
  10. return null;
  11. };