Nested Structures
Learn JavaScript nested structures by building a weekly meal planner that organizes meals by day and type, calculates daily and weekly calorie totals.
You’ve been tracking calories by day and meal type, but now you want to see the whole week’s nutrition in one system. Instead of running your tracker day by day, you need a planner that stores all 7 days of meals, calculates per-meal totals, and shows both daily summaries and a weekly summary.
Typing just:
Day 1 Breakfast: 400Day 1 Lunch: 600Day 1 Dinner: 300
It is useful, but it doesn’t give you a complete weekly overview. What you need is a nested structure, a way to combine lists and dictionaries so data is organized by day and meal category.
Your project: Build “Weekly Meal Planner” that organizes meals by day (keys), meal type (sub-keys), and calories (arrays). Then calculate per-meal totals, daily totals, and the final weekly total.
The programming concept
Next, let’s look at nested structures. A nested structure is when one data structure is contained inside another—for example, an object that holds arrays, or an object made up of other objects.
Example:
let week = {"Day 1": {Breakfast: [400, 350],Lunch: [600],Dinner: [500, 300]},"Day 2": {Breakfast: [450],Lunch: [650, 300],Dinner: [400]}};
Explanation:
...