Untitled

 avatar
user_8207081
plain_text
a year ago
821 B
6
Indexable
const filteredProducts = products.filter(product => (
  (product.name.toLowerCase().includes(filterText.toLowerCase()) &&
   (inStockOnly ? product.stocked : true)) || (!inStockOnly)
));

const organizedProducts = filteredProducts.reduce((accumulator, currentProduct) => {
  const isDifferentCategory = currentProduct.category !== accumulator.lastCategory;

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

  accumulator.lastCategory = isDifferentCategory ? currentProduct.category : accumulator.lastCategory;

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

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