Say I have a search term, const searchTerms = ['sarasota', 'fl']
And a list of locations,
const selectedRestaurants = [{
city: "Orlando",
name: "Dons Food Place",
state: "FL",
id: 1
},
{
city: "Sarasota",
name: "Rons World",
state: "FL",
id: 2
}]
If I search 'Sarasota FL', I want to only return the one Sarasota FL location. But if I'm searching based on multiple parameters (City and State) the search returns both locations, because it matches the state during my second iteration. EDIT: I also need this to work when I search only 'Sarasota'
Here is my code:
filterBySearchTerm(searchTerms) {
let searchedRestaurants = [];
for (var j = 0; j < selectedRestaurants.length; j++) {
searchTerms.forEach(term => {
if (term.toLowerCase() === this.state.selectedRestaurants[j].city.toLowerCase()) {
searchedRestaurants.push(this.state.selectedRestaurants[j])
}
if (term.toLowerCase() === this.state.selectedRestaurants[j].state.toLowerCase()) {
searchedRestaurants.push(this.state.selectedRestaurants[j])
}
if (term.toLowerCase() === this.state.selectedRestaurants[j].name.toLowerCase()) {
searchedRestaurants.push(this.state.selectedRestaurants[j])
}
})
}
//I use Set to remove duplicates ^^
searchedRestaurants = [...new Set(searchedRestaurants)]
return searchedRestaurants;
}
I've been stuck on this for a bit now...can anyone help? Might not make a difference but I'm in ReactJS.