A
A
Anton2020-07-14 14:54:00
JavaScript
Anton, 2020-07-14 14:54:00

How to iterate over an array using the map method with a condition?

I iterate over the array and display the component based on it:

tasks.map((task) => (
   <Task
        key={task.ID}
        task={task}
    />
))


How can I set a condition, for example task.EX == 5, and so that only such array elements get into mine?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2020-07-14
@zakharoffam

First filter the array and then map:

tasks.filter(task => task.EX === 5).map(task => (
   <Task key={task.ID} task={task} />
))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question