Reputation: 21
Sorry if it's been asked before but i couldn't find a solution despite searching in the website. I have an array of objects (restaurants) with many properties. One of them (tags) is an array of string. Then i have another array of strings (filters). The result i need after filtering is an array of restaurants whose tags includes all the filters (this is made reactive with VueJS, there can be any amount of filters including zero).
I'm pretty sure there is a simple solution that i'm currently missing, combining some loops, filter() and some()/includes().
Here is the closest I've been to the result i want (in what probably is a very inefficient way), but it's buggy if you choose some pattern of filter in a certain combination and order(it splice a restaurant that shouldn't be spliced, I have no clue why).
if (restaurants) {
filters.forEach((filter) => {
for (let i = 0; i < restaurants.length; i++) {
const restaurant = restaurants[i]
if (!checkedFilters.includes(filter)) {
checkedFilters.push(filter);
}
if (restaurant.tags.some((tag) => tag.name === filter)) {
if (!filteredRestaurants.includes(restaurant)) {
filteredRestaurants.push(restaurant)
}
}
}
})
for (let i = 0; i < filteredRestaurants.length; i++) {
const filteredRestaurant = filteredRestaurants[i];
checkedFilters.forEach((checkedFilter) => {
if (!filteredRestaurant.tags.some((tag) => tag.name === checkedFilter)) {
filteredRestaurants.splice(i, 1)
}
})
}
return filteredRestaurants
}
},
Upvotes: 2
Views: 1411
Reputation: 22227
I'm not sure why you have a loop to remove things. Does filteredRestaurants already contain restaurants before running this code? If not, try:
Edit: modified to only match restaurants that pass every filter condition.
const restaurants = [
{name: "r1", tags: ["fastfood", "chicken", "fish"]},
{name: "r2", tags: ["steak", "chicken", "seafood"]},
{name: "r3", tags: ["fastfood", "burger", "sandwich"]},
{name: "r4", tags: ["steak", "burger", "sandwich"]}
];
const filters = ["steak", "seafood"];
let filteredRestaurants = restaurants.filter(r => filters.every(f => r.tags.includes(f)));
console.log(filteredRestaurants);
Upvotes: 1