1 var arrayFilter = require('./_arrayFilter'),
2 baseFilter = require('./_baseFilter'),
3 baseIteratee = require('./_baseIteratee'),
4 isArray = require('./isArray');
7 * Iterates over elements of `collection`, returning an array of all elements
8 * `predicate` returns truthy for. The predicate is invoked with three arguments:
9 * (value, index|key, collection).
13 * @category Collection
14 * @param {Array|Object} collection The collection to iterate over.
15 * @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.
16 * @returns {Array} Returns the new filtered array.
20 * { 'user': 'barney', 'age': 36, 'active': true },
21 * { 'user': 'fred', 'age': 40, 'active': false }
24 * _.filter(users, function(o) { return !o.active; });
25 * // => objects for ['fred']
27 * // The `_.matches` iteratee shorthand.
28 * _.filter(users, { 'age': 36, 'active': true });
29 * // => objects for ['barney']
31 * // The `_.matchesProperty` iteratee shorthand.
32 * _.filter(users, ['active', false]);
33 * // => objects for ['fred']
35 * // The `_.property` iteratee shorthand.
36 * _.filter(users, 'active');
37 * // => objects for ['barney']
39 function filter(collection, predicate) {
40 var func = isArray(collection) ? arrayFilter : baseFilter;
41 return func(collection, baseIteratee(predicate, 3));
44 module.exports = filter;