documentcloud/underscore — pull request #1169 by evanp (2013-06-28 02:22:25+00:00)
Title: Add _.deep() to provide deep access to tree properties

With large, complicated data structures -- especially ones that come from JSON -- I often find myself writing code to guard against accessing missing parts of a tree-like object, like so:

```javascript
if (user.profile && user.profile.images && user.profile.images.small) {
return user.profile.images.small.url;
} else {
return undefined;
}
```

This is tiring and error-prone and brittle in the face of data-structure changes. Coffeescript handles this problem with the .? operator. The _.deep() function provides deep access to an object and its properties and does the guard automatically:

```javascript
return _.deep(user, "profile.images.small.url");
```

This will return `undefined` if the `user` object doesn't have an `profile` property, where `user.profile.images.small.url` would cause a `TypeError`.

_.deep() can take a dotted string to find the property, or an array of property names (in case your properties have dots in them!):

```javascript
return _.deep(user, ["profile", "images", "small", "url"]);
```

It's also nice for templates:

```
" />
```