I have a recipes array filled with objects. An object looks like this
var recipes = [
{
name: 'All-Purpose Rub for Meat',
ingredients: [
{
name: 'white sugar',
quantity: {
number: 4,
unit: 'tablespoon'
},
},
{
name: 'salt',
quantity: {
number: 4,
unit: 'tablespoon'
}
},
{
name: 'paprika',
quantity: {
number: 1,
unit: 'tablespoon'
}
},
{
name: 'mustard powder',
quantity: {
number: 2,
unit: 'tablespoon'
}
},
{
name: 'ground black pepper',
quantity: {
number: .5,
unit: 'teaspoon'
}
},
{
name: 'dried oregano',
quantity: {
number: 1,
unit: 'pinch'
}
},
{
name: 'dried thyme',
quantity: {
number: 1,
unit: 'pinch'
}
},
],
servingSize: {
quantity: 12,
unit: 'servings'
},
// usedFor: [ribs, chicken, pork, fish, beef]
},
{
name: 'Rosemary Garlic Rub',
ingredients: [{
name: 'ground black pepper',
quantity: {number: 1, unit: 'tablespoon'}},
{
name: 'kosher salt',
quantity: {number: 1, unit: 'tablespoon'}},
{
name: 'chopped fresh rosemary',
quantity: {number: 3, unit: 'tablespoon'}},
{
name: 'dried rosemary',
quantity: {number: 1, unit: 'tablespoon'}},
{
name: 'diced garlic',
quantity: {number: 8, unit: 'cloves'}},
{
name: 'olive oil',
quantity: {number: .333, unit: 'cup'}}
],
servingSize: {quantity: 4, unit: 'servings'},
// usedFor: [chicken, pork]
},
]
What I want to do is filter the recipes array based on an inputted spice, so it would be something like this
filterRecipes({name: 'ground black pepper'});
I've tried
function filterRecipes(spice) {
let filteredarray = recipes.filter((recipe) => {
recipe.ingredients.includes(spice.name);
});
return filteredarray;
}
console.log(filterRecipes({name: 'kosher salt'}))
The filterRecipes function would then return an array of all recipes where it had the ingredient ' ground black pepper.
Any help would be greatly appreciated.
It looked like I would need something similar to his, but I cant use some because I need all I dont know why its flagging the below as the correct answer, but that doesnt give me back all the items with the ingredients
Filtering array of objects by searching nested object properties
It should return an array of recipes with only objects that have the ingredient that was put as a parameter