Untitled

 avatar
user_3351196
javascript
2 months ago
550 B
1
Indexable
Never
  const finalList = products
  .filter(el => 
    el.name.toLowerCase().includes(filterText.toLowerCase()) && 
    (inStockOnly ? el.stocked : true)
  )
  .reduce((a, c) => {
    const isCategoryChanged = c.category !== a.lastCategory
    a.output = [
      ...a.output,
      isCategoryChanged && <ProductCategoryRow key={c.category} category={c.category} />,
      <ProductRow key={c.name} product={c} />
    ];
    a.lastCategory = isCategoryChanged ? c.category : a.lastCategory
    return a
  }, { lastCategory: null, output: [] })
Leave a Comment