Untitled
user_8207081
plain_text
2 years ago
821 B
9
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