I have to convert 2 functions, both using filter methods into something using for loops, how am i supposed to do that ? For some case, it makes sense, but using the push method is a bit confusing.
Thanks in advance
First function :
function filterWithInputValue(recipes) {
filteredRecipes = recipes.filter(recipe => {
const lowerCaseName = recipe.name.toLowerCase().includes(input.value.toLowerCase());
const lowerCaseDescription = recipe.description.toLowerCase().includes(input.value.toLowerCase());
const lowerCaseIngredients = recipe.ingredients.filter(({ ingredient }) => ingredient.toLowerCase().includes(input.value.toLowerCase())).length > 0
return lowerCaseIngredients | lowerCaseName | lowerCaseDescription;
});
};
My current version of the function :
function filterWithInputValue(recipes) {
for (let recipe of recipes) {
const lowerCaseName = recipe.name.toLowerCase().includes(input.value.toLowerCase());
const lowerCaseDescription = recipe.description.toLowerCase().includes(input.value.toLowerCase());
for (let ingredient of recipe.ingredients) {
//????
}
if (lowerCaseName | lowerCaseDescription | lowerCaseIngredient) {
filteredRecipes.push(recipe)
};
};
};
Second function :
if(tags.length >= 1) {
if(input.value.length >= 3) filterWithInputValue(recipes)
else filteredRecipes = recipes
const tagList = document.querySelectorAll(".tag");
tagList.forEach((tag) => {
if(tag.classList.contains("tag0")) {
filteredRecipes = filteredRecipes.filter(recipe => recipe.ingredients.some(
({ingredient}) => ingredient.includes(tag.innerText)))
}
if(tag.classList.contains("tag1")) filteredRecipes = filteredRecipes.filter(recipe => recipe.appliance.includes(tag.innerText))
if(tag.classList.contains("tag2")) filteredRecipes = filteredRecipes.filter(recipe => recipe.ustensils.includes(tag.innerText))
})
createRecipeCard(filteredRecipes)
mainInputFiltering(filteredRecipes)
};
Example of a recipes/data :
{
"id": 1,
"name" : "Limonade de Coco",
"servings" : 1,
"ingredients": [
{
"ingredient" : "Lait de coco",
"quantity" : 400,
"unit" : "ml"
},
{
"ingredient" : "Jus de citron",
"quantity" : 2
},
{
"ingredient" : "Crème de coco",
"quantity" : 2,
"unit" : "cuillères à soupe"
},
{
"ingredient" : "Sucre",
"quantity" : 30,
"unit" : "grammes"
},
{
"ingredient": "Glaçons"
}
],
"time": 10,
"description": "Mettre les glaçons à votre goût dans le blender, ajouter le lait, la crème de coco, le jus de 2 citrons et le sucre. Mixer jusqu'à avoir la consistence désirée",
"appliance": "Blender",
"ustensils": ["cuillère à Soupe", "verres", "presse citron" ]
}