collection/filter.jsjavascript
import { entries, toIteratee } from '../_internal/iteratee'
/**
* 返回满足条件的集合元素。
* @function filter
* @memberof module:collection
* @param {Array|Object} collection 要遍历的集合
* @param {Function|string|number|Array|Object} [predicate] 判断函数或简写
* @returns {Array} 满足条件的元素数组
* @throws {TypeError} 集合或 predicate 类型不正确时抛出
*/
function filter(collection, predicate) {
const fn = toIteratee(predicate)
return entries(collection).filter(([key, value]) => fn(value, key, collection)).map(([, value]) => value)
}
export default filter