1 var baseEach = require('./internal/baseEach'),
2 baseFind = require('./internal/baseFind'),
3 baseFindIndex = require('./internal/baseFindIndex'),
4 baseIteratee = require('./internal/baseIteratee'),
5 isArray = require('./isArray');
8 * Iterates over elements of `collection`, returning the first element
9 * `predicate` returns truthy for. The predicate is invoked with three arguments:
10 * (value, index|key, collection).
14 * @category Collection
15 * @param {Array|Object} collection The collection to search.
16 * @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.
17 * @returns {*} Returns the matched element, else `undefined`.
21 * { 'user': 'barney', 'age': 36, 'active': true },
22 * { 'user': 'fred', 'age': 40, 'active': false },
23 * { 'user': 'pebbles', 'age': 1, 'active': true }
26 * _.find(users, function(o) { return o.age < 40; });
27 * // => object for 'barney'
29 * // using the `_.matches` iteratee shorthand
30 * _.find(users, { 'age': 1, 'active': true });
31 * // => object for 'pebbles'
33 * // using the `_.matchesProperty` iteratee shorthand
34 * _.find(users, ['active', false]);
35 * // => object for 'fred'
37 * // using the `_.property` iteratee shorthand
38 * _.find(users, 'active');
39 * // => object for 'barney'
41 function find(collection, predicate) {
42 predicate = baseIteratee(predicate, 3);
43 if (isArray(collection)) {
44 var index = baseFindIndex(collection, predicate);
45 return index > -1 ? collection[index] : undefined;
47 return baseFind(collection, predicate, baseEach);
50 module.exports = find;