У меня есть такой массив:
const state = {
products: [
{ name: "Potato", amount: "3"},
{ name: "Butter", amount: "1000" },
{ name: "Salt", amount: "2000" },
//{name: "Egg", amount: "10"},
{ name: "Tomato", amount: "5"},
{ name: "Sour Milk", amount: "5"}
],
recipes: [
{
name: "Mashed potatoes",
ingredients: [
{ name: "Potato", amount: "5"},
{ name: "Butter", amount: "30"},
{ name: "Salt", amount: "15"}
],
instructions: "Some Text"
},
{
name: "Tomato Omelette",
ingredients: [
{ name: "Tomato", amount: "1" },
{ name: "Egg", amount: "1" },
{ name: "Salt", amount: "10" },
{ name: "Butter", amount: "40" }
],
instructions: "Some text"
}
]
};
Я хочу отфильтровать массив моих рецептов по рецептам, которые я могу приготовить из моих продуктов (в этом случае я не могу готовить "Томатный омлет «потому что у меня нет яиц, и я не могу готовить« Картофельное пюре », потому что мне не хватает картошки) .
До сих пор я пробовал разные подходы, но я не пришел с целым решением.
Моим самым близким решением было следующее:
const filterRecipes = (filter, products, recipes) => {
if(filter === 'available products') {
//Getting all product names for future tests
const productsNames = products.map(item => item.name);
//Here we will filter all our recipes by available products
const result = recipes.filter(recipe => {
//Getting all ingredient names of the current recipe
const ingredientNames = recipe.ingredients.map(item => item.name);
//If we have all products for our recipe
//we will add it to our filtered array
if (ingredientNames.every((name) => productsNames.includes(name))){
return true;
}
})
console.log(result);
}
};
Это работает только для названий продуктов, но не для их количества. Когда я пытаюсь проверить сумму, которую он только что сломал.
Вот весь код:
const state = {
products: [
{ name: "Potato", amount: "5"},
{ name: "Butter", amount: "1000" },
{ name: "Salt", amount: "2000" },
//{name: "Egg", amount: "10"},
{ name: "Tomato", amount: "5"},
{ name: "Sour Milk", amount: "5"}
],
recipes: [
{
name: "Mashed potatoes",
ingredients: [
{ name: "Potato", amount: "5"},
{ name: "Butter", amount: "30"},
{ name: "Salt", amount: "15"}
],
instructions: "Some Text"
},
{
name: "Tomato Omelette",
ingredients: [
{ name: "Tomato", amount: "1" },
{ name: "Egg", amount: "1" },
{ name: "Salt", amount: "10" },
{ name: "Butter", amount: "40" }
],
instructions: "Some text"
}
]
};
const filterRecipes = (filter, products, recipes) => {
if(filter === 'available products') {
//Getting all product names for future tests
const productsNames = products.map(item => item.name);
//Here we will filter all our recipes by available products
const result = recipes.filter(recipe => {
//Getting all ingredient names of the current recipe
const ingredientNames = recipe.ingredients.map(item => item.name);
//If we have all products for our recipe
//we will add it to our filtered array
if (ingredientNames.every((name) => productsNames.includes(name))){
return true;
}
})
console.log(result);
}
};
filterRecipes("available products", state.products, state.recipes);