Angular 如何过滤ngrx中的实体?
我创建了一个ngrx实体服务:Angular 如何过滤ngrx中的实体?,angular,ngrx,Angular,Ngrx,我创建了一个ngrx实体服务: @Injectable({ providedIn: 'root' }) export class ProductsDataService extends EntityCollectionServiceBase<{ id, name, isActive }> { actives$ = this.entities$.pipe(filter(e => e.isActive); <------ but e is an Array! @Inje
@Injectable({ providedIn: 'root' })
export class ProductsDataService extends EntityCollectionServiceBase<{ id, name, isActive }> {
actives$ = this.entities$.pipe(filter(e => e.isActive); <------ but e is an Array!
@Injectable({providedIn:'root'})
导出类ProductsDataService扩展EntityCollectionServiceBase{
actives$=this.entities$.pipe(filter(e=>e.isActive);映射函数是正确的方法:
this.entities$.pipe(map(e => e.filter(...));
Filter用于过滤数据流,而不是过滤数据本身,即发出通过提供条件的值(参考:learnrxjs.io/learn rxjs/operators/Filter/Filter)。换句话说,决定数据流是否仅在特定条件下工作
一个简单的例子:
x.pipe(filter(num => num === 2)).subscribe(val => console.log(val))
只有当x发出2时才会打印。我开始使用rxjs时也有同样的困惑。在管道的上下文中,操作符映射和过滤器对流进行操作,而不是对传递到流中的数组进行操作。因此map
允许您将数组更改为其他数组。因此,您希望使用rxjsmap
来更改将数组转换为另一个数组,并对映射对象调用jsarray.filter()
,将数组更改为仅包含流输出活动项的数组。