角度可观察过滤

我遇到了使用数组过滤可观察对象的问题。 让我解释 ...

我有:

  • products $:可观察;
  • filterProducts $:可观察;
  • selectedFilters = [['过滤器名称',idOfFilter],['过滤器名称',idOfFilter]]; (开头为空)

Here is the structure of a product object: Here is the structure of a product object

And here is the structure of my filters array enter image description here

我想将过滤的结果设置为filteredProducts。 对于过滤器,我必须检查产品的过滤器数组是否包含过滤器的名称,以及产品值数组的是否包含过滤器ID。

到目前为止,我一直在写这篇文章,但我不知道如何真正过滤我的Observable ...

export class ProductsFilterComponent extends BaseComponent implements OnInit {
    @Select(FiltersState.getAllFilters) filters$: Observable<any>;
    @Input() products$: Observable<Product[]>;
    filteredProducts$: Observable<Product[]>;

    public tempProducts$: Observable<Product[]>;
    public selectedFilters = [];

    constructor(
        private store: Store) { super(); }

    ngOnInit() {
        this.store.dispatch(new GetAllFilters());
        this.uns = this.products$.subscribe(res => console.log('products from filter component', res));
    }

    private filterProducts() {
      this.filteredProducts$ = this.products$.pipe(
          map(productsArray =>
              productsArray.filter(product => {
                  product.filters.filter(filters => filters.values.includes(5));
              }))
      );
  }

}
评论