Untitled

 avatar
user_2672984
plain_text
a month ago
755 B
0
Indexable
Never
const filteredProducts = products.filter(product => {
    let productName = product.name.toLowerCase().indexOf(filterText.toLowerCase());
    return productName !== -1 && ((inStockOnly && product.stocked) || !inStockOnly);
});

const categorizedProducts = filteredProducts.reduce((accumulator, currentProduct) => {
    currentProduct.category !== accumulator.lastCategory ?
        accumulator.output.push(
            <ProductCategoryRow key={currentProduct.category} category={currentProduct.category} />
        ) : null;
    accumulator.lastCategory = currentProduct.category;
    accumulator.output.push(<ProductRow key={currentProduct.name} product={currentProduct} />);
    return accumulator;
}, { lastCategory: null, output: [] });
Leave a Comment