N
N
nivaech2019-01-31 09:43:15
JavaScript
nivaech, 2019-01-31 09:43:15

How to render an array of objects with only one parameter using map?

I have an array with data that looks something like this:

const products = [
    {
        id: 1,
        title: "Some title",
        category: "Category One"
    },
     {
        id: 2,
        title: "Some title",
        category: "Category One"
    },
    {
        id: 3,
        title: "Some title",
        category: "Category Two"
    },
     {
        id: 4,
        title: "Some title",
        category: "Category Two"
    }];

In one array there are products of different categories, but I need to pull out and display products of only one category. Now it all looks like this to me, but this option is incorrect, because it displays the entire array on the screen.
const itemList = products.map(item => <ProductCard
        key = {item.id}
        title = {item.title}
        category = {item.category}
        />)

How to render part of an array with only Category One using map, ignoring Category Two?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Proskurin, 2019-01-31
@nivaech

Use filter . Or you can place a conditional construct (with the && operator)

const itemList = products.map(item => item.category === "Category Two" && <ProductCard
        key = {item.id}
        title = {item.title}
        category = {item.category}
        />)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question