This is what I have so far and what I've and tried:
groceryList = [];
ngOnInit() {
this._recipesSub = this.recipesService.recipes.subscribe((receivedData) => {
this.loadedRecipes = receivedData.recipes;
});
}
onCheckRecipe(e) {
if (e.detail.checked === true) {
let tickedRecipe = e.detail.value;
let selectedRecipeIngredients;
for (let recipe of this.loadedRecipes) {
if (recipe.name === tickedRecipe) {
selectedRecipeIngredients = recipe.ingredients;
}
}
for (let selectedIng of selectedRecipeIngredients) {
for (let existingIng of this.groceryList) {
if (selectedIng.name) {
this.groceryList.push(selectedIng);
}
}
}
console.log(this.groceryList);
}
}
This is what I receive in my console:
(8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {_id: "5f64ac1eb227fea1f63a5c99", name: "chia seeds", quantity: "10g"}
1: {_id: "5f64ac1eb227fea1f63a5c9a", name: "cherries", quantity: "15g"}
2: {_id: "5f64ac1eb227fea1f63a5c9b", name: "honey", quantity: "30g"}
3: {_id: "5f64ac1eb227fea1f63a5c9c", name: "almond flour", quantity: "10g"}
4: {_id: "5f64ac1eb227fea1f63a5c9e", name: "almond flour", quantity: "10g"}
5: {_id: "5f64ac1eb227fea1f63a5c9f", name: "egg", quantity: "10g"}
6: {_id: "5f64ac1eb227fea1f63a5ca0", name: "unsalted butter", quantity: "30g"}
7: {_id: "5f64ac1eb227fea1f63a5ca1", name: "peanut butter", quantity: "50g"}
length: 8
__proto__: Array(0)
and my desire outcome is not to have name: "almond flour" twice because it was already in the array, so i can be able to add just the object that which name value doesn't exist already:
Instead of 3:
{_id: "5f64ac1eb227fea1f63a5c9c", name: "almond flour", quantity: "10g"}, {_id: "5f64ac1eb227fea1f63a5c9e", name: "almond flour", quantity: "10g"} to be {_id: "5f64ac1eb227fea1f63a5c9e", name: "almond flour", quantity: "20g"}
What I want to achieve is to add to my groceryList array each Ingredient only if that ingredient is not present already and if it's present to add just the quantity of that ingredient to an existing key. For example if i have the ingredient {_id: "5f64ac1eb227fea1f63a5c99", name: "chia seeds", quantity: "10g"} to add just the quantity and not to create a new obj of ingredient in my groceryList
I am don't know what approach to take.. If you guys have any idea I would really appreciate it.
** Also the remove option
onCheckRecipe(e) {
if (e.detail.checked === true) {
for (let recipe of this.loadedRecipes) {
if (recipe.name === e.detail.value) {
recipe.ingredients.forEach((eachIngredient) => {
let matchedIng = this.groceryList.find(function (foundIng) {
return foundIng.name === eachIngredient.name;
});
if (matchedIng) {
matchedIng.quantity =
matchedIng.quantity + eachIngredient.quantity;
} else {
this.groceryList.push(eachIngredient);
}
});
}
}
} else {
for (let recipe of this.loadedRecipes) {
if (recipe.name === e.detail.value) {
recipe.ingredients.forEach((element) => {
let matched = this.groceryList.find(function (foundIngre) {
return foundIngre.name === element.name;
});
if (matched.quantity === element.quantity) {
let index = this.groceryList.findIndex(
(x) => x.name === matched.name
);
this.groceryList.splice(index, 1);
} else {
matched.quantity = matched.quantity - element.quantity;
}
});
}
}
}
}