I'm trying to filter an observable of products with an array of filters but i really don't know how..
Let me explain, I would like to set the result of my filtering to filteredProducts. For filtering i have to check, for each filter, if the product's filter array contains the name of the filter and if the products values array's contains filter id.
For the moment, the filter works but only with the last selected filter and i'd like to filter products list with all filters in my selectedFilters array. I can have one or multiple filters.
export class ProductsFilterComponent extends BaseComponent implements OnInit {
@Select(FiltersState.getAllFilters) filters$: Observable<any>;
@Input() products$: Observable<Product[]>;
filteredProducts$: Observable<Product[]>;
public selectedFilters = [];
constructor(
private store: Store) { super(); }
ngOnInit() {
this.store.dispatch(new GetAllFilters());
}
private filterProducts() {
this.filteredProducts$ = this.products$.pipe(
map(
productsArr => productsArr.filter(
p =>
p.filters.some(f => this.selectedFilters.some(([selectedF]) => selectedF === f.name.toLowerCase()) // Filter name
&& f.values.some(value => this.selectedFilters.some(([, filterId]) => filterId === value)) // Filter id
)
)
)
);
this.filteredProducts$.subscribe(res => console.log('filtered:', res));
}
}
Here's the structure of a product object

Here's the structure of selectedFilters

A big thank you in advance :-).
filteredProductsthat uses both the products and filters from the store.GetAllFiltersandFiltersState. So if there is something wrong it is easier to spot. Also you have aBaseComponentnot sure if it is relevant for the issue. In a nut-sell make it easier for the other users to find the issue without having to make assumptions.