Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Fantastic library to easily get to nested properties of objects in JavaScript (github.com/d10n)
2 points by softinio on June 24, 2015 | hide | past | favorite | 1 comment


  Object.prototype.get = function(k, d){
    return k.split(".").reduce(function(a, b){
      return a ? b in a && a[b] || d : a 
    }, this)
  };

  // ({}).get('a.b.c', null) === null
  // ({a:{b:{c:2}}}).get('a.b.c') === 2




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: