🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## filter + [link](./filter "Link to this entry.") + [source](https://github.com/lodash/lodash/blob/4.5.0正式版/lodash.src.js#L7582 "View in source.") + [npm](https://www.npmjs.com/package/lodash.filter "See the npm package.") ``` _.filter(collection, [predicate=_.identity]) ``` 遍历集合中的元素,筛选出一个经过 `predicate` 检查结果为真值的数组,predicate 会传入3个参数:(value, index|key, collection)。 ### 参数 1. collection (Array|Object) 需要遍历的集合 2. [predicate=_.identity] (Function|Object|string) 这个函数会处理每一个元素 ### 返回值 (Array) 返回筛选结果的新数组 ### 示例 ``` 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'] // 使用了 `_.matches` 的回调结果 resolve( _.filter(users, { 'age': 36, 'active': true }) ); // => ['barney'] // 使用了 `_.matchesProperty` 的回调结果 resolve( _.filter(users, ['active', false]) ); // => ['fred'] // 使用了 `_.property` 的回调结果 resolve( _.filter(users, 'active') ); // => ['barney'] ```