I am learning React Native by building a RideSharing app. I have a function that allows the user to filter a FlatList
component with several ride and each one of them has different items
associated to, sutch as date, number of passengers, cost, etc... And filteredDate
, filteredPassangers
, filteredCost
are state variables that allow the user to choose how he wishes to filter.
I wrote the function that filters the data like this:
function filter() {
const newData1 = lists.filter(
item => {
return item.date === filteredDate,
item.To === filteredTo,
item.passangers === filteredPassangers,
item.date === filteredDate,
item.cost3 === filteredCost
})
props.setLists(newData1),
//...
}
However, when I change the state for more than one variable, it doesn't return any ride component what so ever.
How can I code this in order to filter through multiple items at a time?
inside your comparison statement you have separated them with comma. That way only the last statement is being evaluated that is item.cost3 === filteredCost
you can filter them using or ( || )
operator like this
item.date === filteredDate ||
item.To === filteredTo ||
item.passangers === filteredPassangers ||
item.date === filteredDate ||
item.cost3 === filteredCost
that way if any of your conditions are true it will return true.