jashkenas/underscore — pull request #1472 by jasonLaster (2014-02-12 23:41:53+00:00)
Title: Update pick and omit to accept a predicate

Here's a small change that would add support for using predicates with `pick` and `omit`. The interface would support `_.pick(obj, 'key1', 'key2')` and `_.pick(obj, predicate)`. It's based on the discussion from #359 #833. If this feels like interface bloat, I think `_.subset(obj, predicate)` would be a good alternative.

Regardless of the api, adding an easy option for getting a subset of an object feels right. Using `reduce` is a lot of boilerplate and unintuitive for many people.

```js
_.reduce(obj, function(memo, v, k) {if(predicate) memo[k] = v; return memo}, {})
```

Here's a small code sample for how `omit` and `pick` would work.

```js
var data = { 'name': 'moe', 'userid': 'moe1' , '_hint': 'knucklehead', '_seed': '96c4eb' };

_.pick(data, 'name');
_.pick(data, function(value, key) { return key.charAt(0) != '_'; });

_.omit(data, 'userid');
_.omit(data, function(value, key) { return key.charAt(0) == '_'; });

// → { 'name': 'moe' }
// → { 'name': 'moe', 'userid': 'moe1' }
// → { 'name': 'moe', 'age': 40, 'userid': 'moe1' }
// → { 'name': 'moe', 'userid': 'moe1' }
```