Untitled

 avatar
unknown
javascript
a year ago
651 B
5
Indexable
const filteredCategories = products.reduce((result, product) => {
  const nameMatches = product.name.toLowerCase().includes(filterText.toLowerCase());
  const isStockMatch = inStockOnly ? product.stocked : true;

  if (nameMatches && isStockMatch) {
    if (product.category !== result.lastCategory) {
      result.output.push(<ProductCategoryRow key={product.category} category={product.category} />);
      result.lastCategory = product.category;
    }
    result.output.push(<ProductRow key={product.name} product={product} />);
  }

  return result;
}, { lastCategory: null, output: [] });

console.log(filteredCategories.output);
Editor is loading...
Leave a Comment