I've been trying to do this function for two days and I'm not succeeding. I would like the function to return me the objects that satisfy the filter array. The problem is that I would like it to return only objects that satisfy all filters.
My array of filters:
const myFiltersIngredients = ['farinha', 'frango']
My array of objects:
const myRecipes = [
{
name: 'torta da vovo',
ingredients: [
{
ingredient: 'farinha',
ingredientUnit: 'grama(s)',
ingredientQuantity: 500
},
{
ingredient: 'frango',
ingredientUnit: 'grama(s)',
ingredientQuantity: 500
}
]
},
{
name: 'biscoito',
ingredients: [
{
ingredient: 'farinha',
ingredientUnit: 'grama(s)',
ingredientQuantity: 500
},
{
ingredient: 'manteiga',
ingredientUnit: 'grama(s)',
ingredientQuantity: 500
}
]
}
]
Array.prototype.every()method to test that every ingredient is matched, andArray.protoyype.some()to tell whether any of the ingredients in a recipe uses an ingredient.myRecipes.filter(({ingredients}) => myFiltersIngredients.every(myfilter => ingredients.map(({ingredient}) => ingredient).includes(myfilter)));