Untitled

 avatar
user_4355577
plain_text
2 months ago
598 B
2
Indexable
Never
const filteredProducts = products
  .filter(el => 
    el.name.toLowerCase().includes(filterText.toLowerCase()) &&
    (!inStockOnly || (inStockOnly && el.stocked))
  );

const groupedProducts = filteredProducts.reduce((acc, current) => {
  const { category } = current;
  const isNewCategory = category !== acc.lastCategory;

  acc.output.push(
    isNewCategory
      ? <ProductCategoryRow key={category} category={category} />
      : <ProductRow key={current.name} product={current} />
  );

  acc.lastCategory = category;
  return acc;
}, { lastCategory: null, output: [] });
Leave a Comment