Forum Academy Marketplace Showcase Pricing Features

How To Calculate Sum Of Multiple Items In A List

I have a meal planning app that I need help calculating nutritional information for.

Right now an Ingredient contains Calories, Fats, Proteins, Carbs, Reference Amount, Reference Measurement. (ex. Fresh Ginger, Calories = 2, Fats = 0g, Protein = 0g, Carbs = 0.4g, Amount = 1, Measurement = Teaspoon). This means for every 1 tsp of ginger, you’ll get 2 calories, 0g fat, etc.

I then have Recipe Ingredients that contain an Ingredient along with Quantity and Measurement. (ex. 3 teaspoons of Fresh Ginger).

I want to be able to calculate all of the Calories, Proteins, Carbs and Fats for a Recipe by adding up that nutritional information from the Ingredients (divided by the quantity of the Recipe Ingredient) inside the Recipe Ingredients inside the Recipe.

The formula to find protein of a Recipe Ingredient is
(Recipe Ingredient Quantity / Ingredient Amount) * Ingredient Protein

Then you take that output, sum up them for all Recipe Ingredients and you get the Protein amount for the entire Recipe

Hope that makes sense, don’t know if this should be a frontend calculation or DB calculation. Any help is much appreciated!

1 Like