ThinkinginReact12+ | BY Nattpon

mail@pastecode.io avatar
unknown
javascript
2 months ago
580 B
4
Indexable
Never
 const finalList = products
  .filter(el => el.name.toLowerCase().includes(filterText.toLowerCase()) && (!inStockOnly || el.stocked))
  .reduce((result, product) => {
    const { output, lastCategory } = result;
    const isCategoryChanged = product.category !== lastCategory;
    output.push(isCategoryChanged ? <ProductCategoryRow key={product.category} category={product.category} /> : <ProductRow key={product.name} product={product} />);
    return { lastCategory: product.category, output };
  }, { lastCategory: null, output: [] });
console.log(finalList.output);
Leave a Comment