Untitled

mail@pastecode.io avatar
unknown
javascript
2 months ago
583 B
12
Indexable
Never
const finalList = products
  .filter(el => 
    (el.name.toLowerCase().indexOf(filterText.toLowerCase()) !== -1) &&
    (!inStockOnly || (inStockOnly && el.stocked))
  )
  .reduce((a, c) => {
    const isDifferentCategory = c.category !== a.lastCategory;

    a.output = [ ...a.output, isDifferentCategory ? <ProductCategoryRow key={c.category} category={c.category} /> : null,
      <ProductRow key={c.name} product={c} />].filter(Boolean);
    a.lastCategory = isDifferentCategory ? c.category : a.lastCategory;
    return a;
  }, { lastCategory: null, output: [] });
Leave a Comment