0

I want to be able to filter products based on one of a, b, c, or d matching the value of `this.listFilter'

I think the code below is almost doing what I want, the split function returns an array but only seems to be matching on the first element in the array, I need to iterate over each element and check if it matches the value of this.listFilter.

The breadcrumb array contains:

breadcrumb[ 'a > b > c > d' ]

return this.products.filter((product: IProduct) => product.categories.category.breadcrumb[0].split(' > ').indexOf(this.listFilter));

Can anyone suggest a way to iterate over the array split by the " > " and check against the value of this.listFilter?

user1532669
  • 2,288
  • 4
  • 36
  • 72
  • 1
    [`.indexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) returns the index or `-1`, you'll have to add a check like `.indexOf(..) != -1` or `.indexOf(..) >= 0` – Titus Nov 04 '19 at 19:48
  • Or, replace `indexOf` with `includes` – adiga Nov 04 '19 at 19:50
  • Thank you very much, sincerely appreciated :) – user1532669 Nov 05 '19 at 17:17

0 Answers0