Homework12+

user_3507779 avatar
user_3507779
javascript
01/02/2024 6:14 PM
800 B
20
Indexable
const myFilter = products.filter(el => (el.name.toLowerCase().includes(filterText.toLowerCase()) &&
                (inStockOnly ? el.stocked : true) || (!inStockOnly)));
const finalList = myFilter.reduce((a, c) => {
const isDifferentCategory = c.category !== a.lastCategory;

  a.output = [...a.output,isDifferentCategory && <ProductCategoryRow key={c.category} category={c.category} />,
  <ProductRow key={c.name} product={c} />
  ];

  a.lastCategory = isDifferentCategory ? c.category : a.lastCategory;

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

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