In Follow a Path Into Nested Data, you reached a single value, the amount of onion in Grandma Ines’s lentil soup, by following recipes[0].ingredients[1].amount one step per level. Most of Sam’s questions are about many values at once, though. Which recipes need a trip to the shop, and which are mains? And now that the meal planner holds three meals a day, what’s planned for dinner on Wednesday?
In this lesson, you’ll choose a shape for data like this based on the questions your code has to answer. Then you’ll loop over records that have lists inside them to pick some out, find the biggest, and group them, and you’ll work through a grid by rows and by columns. Start with the shape, because it decides what every loop after it looks like.
Choose a Shape From Its Questions
Settle the shape before any code depends on it. About one ingredient, Sam’s box asks for its name, amount, and unit. Those are named parts of one thing, so an ingredient is an object: { name, amount, unit }.
About a recipe’s ingredients as a whole, the questions change: What do I need to buy for this recipe, and how much onion does the week use? Both mean visiting every ingredient in turn, and a recipe can have three or twelve, all of the same kind. That’s what an array is for, so ingredients is an array of objects.
Why not make the ingredients a lookup table like Sam’s pantry, with each name as a key? That would answer “How much onion?” in one step. However, the box asks “What’s in this recipe?” far more often, and a list keeps the order a cook reads them in. The rule of thumb is arrays for many of the same, and objects for named parts or lookups.
Sam’s old meal planner held one dish per day, an array of seven values, Monday first. Now each day includes breakfast, lunch, and dinner, so it becomes a row of three meals. Sketch it one level per line, as the video did for the recipes:
week [ ] seven days, Monday first
day [ ] breakfast, lunch, dinner
meal a recipe's name, or undefined
An array of rows that all have the same length is called a grid, or a two-dimensional array. A meal that isn’t planned yet holds undefined, so every row keeps its three places.
Why are a day’s meals an array and not an object with breakfast, lunch, and dinner keys? An object would read well for one meal, but the planner also asks “How many lunches are planned this week?” With positions, the same index means the same meal in every row, so one number walks down a whole column.
Before you read on, try the same reasoning yourself. Sam wants each recipe to keep the family’s scores out of five, to answer “What did Priya give the soup?” and “What’s its average?” Sketch a shape for one recipe’s scores.
One shape that fits is an object, scores: { Priya: 4, Tomás: 5 }. The first question is a lookup by name, which a key answers in one step, and Object.values() gives you every score to average for the second. An array of { person, score } records works too, but you’d search it for Priya.
Loop Over Recipes and Their Ingredients
Before going shopping, Sam wants to know which recipes need something from the shop, and which recipe needs the most. That means visiting every ingredient of every recipe, with a loop over each recipe’s ingredients inside a loop over the recipes.
Open a new tab at about:blank and switch to its Console. Keep that tab for the whole lesson, because each example builds on the ones before. Start with the soup from the video, trimmed to the fields this lesson needs: