Untitled

mail@pastecode.io avatar
unknown
javascript
2 months ago
526 B
12
Indexable
Never
const myFilter = products.filter(el =>
  el.name.toLowerCase().includes(filterText.toLowerCase()) && (inStockOnly ? el.stocked : true)
);
const finalList = myFilter.reduce((a, c) => {
  const { lastCategory, output } = a;
  const categoryRow = c.category !== lastCategory ? [...output, <ProductCategoryRow key={c.category} category={c.category} />] : output;
  return {
    lastCategory: c.category,
    output: [...categoryRow, <ProductRow key={c.name} product={c} />]
  };
}, { lastCategory: null, output: [] });
Leave a Comment