[英]lodash _.find all matches
I have simple function to return me object which meets my criteria. 我有简单的功能来返回符合我标准的对象。
Code looks like: 代码如下:
var res = _.find($state.get(), function(i) {
var match = i.name.match(re);
return match &&
(!i.restrict || i.restrict($rootScope.user));
});
How can I find all results (not just first) which meets this criteria but all results. 我怎样才能找到符合此标准但所有结果的所有结果(不仅仅是第一个)。
Thanks for any advise. 谢谢你的任何建议。
Just use _.filter
- it returns all matched items. 只需使用_.filter
- 它将返回所有匹配的项目。
Iterates over elements of collection, returning an array of all elements predicate returns truthy for. 迭代集合的元素,返回所有元素的数组谓词返回truthy。 The predicate is invoked with three arguments: (value, index|key, collection). 使用三个参数调用谓词:(value,index | key,collection)。
You can use _.filter, passing in all of your requirements like so: 您可以使用_.filter,传递所有要求,如下所示:
var res = _.filter($state.get(), function(i) {
var match = i.name.match(re);
return match &&
(!i.restrict || i.restrict($rootScope.user));
});
Without lodash using ES6, FYI: 没有使用ES6的lodash,FYI:
Basic example (gets people whose age is less than 30): 基本示例(获取年龄小于30岁的人):
const peopleYoungerThan30 = personArray.filter(person => person.age < 30)
Example using your code: 使用代码的示例:
$state.get().filter(i => {
var match = i.name.match(re);
return match &&
(!i.restrict || i.restrict($rootScope.user));
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.