I'm trying to do a shopping list generator (client selects cocktails and an ingredients list is generated) and I'm just stuck on this part. I have the following code:
There's a 'data' set which contains the arrays for all the cocktails and their ingredients but it's too big to paste here. 'savedData' is the selected cocktails ingredients.
function loadData(savedData) {
let selectedCocktailsHTML = ``;
savedData.cocktailsList.forEach(item => {
let selectedIngredients = ``;
if (item.isChecked) {
item.ingredients.forEach(ingred => {
selectedIngredients += `<p>${ingred.name} - ${ingred.selectedQty} ${ingred.capacity}</p>`;
});
selectedCocktailsHTML += `<p class="review-screen-ingred-name"> ${selectedIngredients}`;
}
});
And it gives me an output like this:
Vodka - 1 bottles 1L
Peach Schnapps - 1 bottles 1L
Cranberry Juice - 2 bottles 1L
Coffee Liqueur - 1 bottles 1L
Vodka - 1 bottles 1L
What I would like to do is to loop through and combine the duplicate values when the user selects them (such as vodka in this case) and then add the quantities of each one together. My desired output for this example would be:
Vodka - 2 bottles 1L
Peach Schnapps - 1 bottles 1L
Cranberry Juice - 2 bottles 1L
Coffee Liqueur - 1 bottles 1L
Can anyone help with the best way to go about this? Theres a lot of the code I didn't add, let me know if you need more to work it out.