filter source npm

_.filter(collection, [predicate=_.identity])

Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments:
(value, index|key, collection).

Arguments

  1. collection (Array|Object)

    The collection to iterate over.

  2. [predicate=_.identity] (Function|Object|string)

    The function invoked per iteration.

Returns (Array)

Returns the new filtered array.

Example

var resolve = _.partial(_.map, _, 'user');

var users = [
  { 'user': 'barney', 'age': 36, 'active': true },
  { 'user': 'fred',   'age': 40, 'active': false }
];

resolve( _.filter(users, function(o) { return !o.active; }) );
// => ['fred']

// using the `_.matches` callback shorthand
resolve( _.filter(users, { 'age': 36, 'active': true }) );
// => ['barney']

// using the `_.matchesProperty` callback shorthand
resolve( _.filter(users, ['active', false]) );
// => ['fred']

// using the `_.property` callback shorthand
resolve( _.filter(users, 'active') );
// => ['barney']