N
N
nivaech2019-05-15 08:49:46
JavaScript
nivaech, 2019-05-15 08:49:46

How to display the number of elements of the map function in JSX?

There is a product page. The map function traverses the array of products from the base, selects the products of a given category ("Some category" ), and displays them on the screen. I want to display the number of displayed products above. How to display the length of the new array created by map?
Here is the code.

<div className="products-list">
                        <Title title="Some Title" />
                        <div className="products-container">
                          <p> "Количество элементов категории в map здесь" </p>
                        </div>
                        <div className="products-container">
                        {filteredProducts.map(product => product.category === "Some, category" && 
                          <ProductCard 
                            key={product.id}
                            product = {product}
                          />
                        )}
                        </div>
                      </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kova1ev, 2019-05-15
@nivaech

const products = filteredProducts.filter(product => product.category === "Some, category").map (item => {
                          return (
                          <ProductCard 
                            key={item.id}
                            product = {item}
                          />)} )
return (
  <div className="products-list">
                        <Title title="Some Title" />
                        <div className="products-container">
                          <p> {products.length} </p>
                        </div>
                        <div className="products-container">
                        {products}
                        </div>
                      </div>
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question