I’m tracking my nutritional information with an entry for each food item I consume using this format:
- [c] (meal:: Lunch): (item:: [[Bibigo Cooked Sticky White Rice]]) (1 Bowl) [cal:: 311], [fat:: 1.1], [carbs:: 71], [protein:: 6.1]
With the help of some CSS it is displayed something like this in my journal note:
🍴 Lunch: Bibigo Cooked Sticky White Rice (1 Bowl) cal = 311, fat = 1.1, carbs = 71, protein = 6.1
I’d like to include a table that summarizes everything using a dataviewjs script that I include at the bottom of every journal page. I’m imagining it would look something like this:
Meal | Calories | Fat | Carbs | Protein |
---|---|---|---|---|
Breakfast | 423 | 15 | 35 | 17 |
Morning Snack | 0 | 0 | 0 | 0 |
Lunch | 640 | 31 | 80 | 12 |
Afternoon Snack | 250 | 12 | 15 | 7 |
Dinner | 750 | 21 | 45 | 25 |
Evening Snack | 400 | 18 | 25 | 17 |
Total | 2463 | 97 | 200 | 78 |
Ideally, if any of the 6 meals didn’t have any entries, it wouldn’t appear in the table.
I found a post at Dataview sum of all prices in table - #2 by smiggles that I thought I could adapt, as shown below, but I get a ‘Pages.map is not a function’ error:
```dataviewjs
let pages = dv.current()
let totals = pages.map((page, index) => [
page.meal,
dv.func.sum(page.cal),
dv.func.sum(page.fat),
dv.func.sum(page.carbs),
dv.func.sum(page.protein)
]);
totals["values"][total.length] = [
"**Total**",
dv.func.default(dv.func.sum(pages.cal),0),
dv.func.default(dv.func.sum(pages.fat),0),
dv.func.default(dv.func.sum(pages.carbs),0),
dv.func.default(dv.func.sum(pages.protein),0),
];
dv.table(["Meal", "Calories", "Fat", "Carbs", "Protein"], totals);
```
Am I on the right path? Any pointers to either get this script working, or an even better way of accomplishing my goal? I’m slowly understanding more DVJS with every project I tackle, but I’m still a long way from competent.