And*_*rit 44 javascript lodash
我有简单的功能来返回符合我标准的对象.
代码如下:
var res = _.find($state.get(), function(i) {
var match = i.name.match(re);
return match &&
(!i.restrict || i.restrict($rootScope.user));
});
Run Code Online (Sandbox Code Playgroud)
我怎样才能找到符合此标准但所有结果的所有结果(不仅仅是第一个).
谢谢你的建议.
您可以使用_.filter,传递所有要求,如下所示:
var res = _.filter($state.get(), function(i) {
var match = i.name.match(re);
return match &&
(!i.restrict || i.restrict($rootScope.user));
});
Run Code Online (Sandbox Code Playgroud)
在不使用ES6和lodash的情况下,仅供参考:
基本示例(获取年龄小于30岁的人):
const peopleYoungerThan30 = personArray.filter(person => person.age < 30)
Run Code Online (Sandbox Code Playgroud)
使用代码的示例:
$state.get().filter(i => {
var match = i.name.match(re);
return match &&
(!i.restrict || i.restrict($rootScope.user));
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
50123 次 |
最近记录: |