gabrysiak
8/14/2014 - 7:41 PM

Javascript - Read Nested Object Property

Javascript - Read Nested Object Property

//
// Usage... for a nested structure
// var test = {
//    nested: {
//      value: 'Read Correctly'
//   }
// };
// safeRead(test, 'nested', 'value');  // returns 'Read Correctly'
// safeRead(test, 'missing', 'value'); // returns ''
//

var safeRead = function() {
  var current, formatProperty, obj, prop, props, val, _i, _len;
 
  obj = arguments[0], props = 2 <= arguments.length ? [].slice.call(arguments, 1) : [];
 
  read = function(obj, prop) {
    if ((obj != null ? obj[prop] : void 0) == null) {
      return;
    }
    return obj[prop];
  };
 
  current = obj; 
  for (_i = 0, _len = props.length; _i < _len; _i++) {
    prop = props[_i];
 
    if (val = read(current, prop)) {
      current = val;
    } else {
      return '';
    }
  }
  return current;
};