collection/find.jsjavascript
import { entries, toIteratee } from '../_internal/iteratee'

/**
 * 返回集合中第一个满足条件的元素。
 * @function find
 * @memberof module:collection
 * @param {Array|Object} collection 要遍历的集合
 * @param {Function|string|number|Array|Object} [predicate] 判断函数或简写
 * @returns {*} 第一个匹配元素;没有匹配时返回 undefined
 * @throws {TypeError} 集合或 predicate 类型不正确时抛出
 */
function find(collection, predicate) {
  const fn = toIteratee(predicate)
  const entry = entries(collection).find(([key, value]) => fn(value, key, collection))
  return entry?.[1]
}

export default find