Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Follow a Path Into Nested Data

In this video, you found how much onion Grandma Ines’s lentil soup needs by following a path into Sam’s recipes, one level at a time. Here are the key points:

  • Sketch the shape before you write a path. Sam’s recipes are nested data: an array of recipe objects, each with an ingredients array of ingredient objects. Arrays and objects take turns, four levels deep.
  • Each step of a path goes down one level. An array level takes square brackets and an index, such as [1], and indexes count from zero. An object level takes a dot and a key, such as .amount, or the key in square brackets, as with Sam’s pantry.
  • Read a path from left to right against the sketch. In recipes[0].ingredients[2].unit, [0] reaches the soup, .ingredients its list, [2] the third ingredient, the stock, and .unit its unit.
  • Watch out for a path that skips a level. recipes is an array, so recipes.ingredients is undefined, and the next step throws a TypeError. In Chrome, its message names the step after the undefined one, (reading '1'), so the fix goes before .ingredients: the recipe’s index, as in recipes[0].ingredients[1].amount.

Here’s how the soup’s record looks once Sam’s box keeps ingredients, a change the box gets later in the course, followed by the two paths from the video:

Language: JavaScript
const recipes = [
  {
    id: "b5115c00-be61-4769-919a-67688c8be942",
    name: "Grandma Ines's lentil soup",
    servings: 6,
    minutes: 45,
    category: "soup",
    vegetarian: true,
    ingredients: [
      { name: "lentils", amount: 200, unit: "g" },
      { name: "onion", amount: 1, unit: "whole" },
      { name: "stock", amount: 1, unit: "l" },
    ],
  },
  // ...Sam's other four recipes, the same shape
];
console.log("Onion:", recipes[0].ingredients[1].amount);
console.log("Unit:", recipes[0].ingredients[2].unit);

Save and reload the page, and the Console shows the onion’s amount and the stock’s unit:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Last lesson, Sam’s pantry worked as a lookup table. One key, and you had an amount. In this video, you’ll reach a value that sits several levels down in Sam’s recipes.

00:12 Sam’s box now keeps each recipe’s ingredients. Here’s the lentil soup’s record, written out in full. It has a new field, ingredients, holding a list. Each ingredient is a small record of its own, with a name, an amount and a unit.

00:26 The other four recipes have the same shape, so only the soup is on screen. Sam is writing a shopping list and wants to know how much onion the soup needs.

00:36 That number sits in a record, inside a list, inside a record, inside a list. One key won’t reach it.

00:46 Before you write a path, sketch the shape, one level per line. The outside level is recipes, an array. Each recipe in it is an object with named fields. One of those fields, ingredients, is another array.

01:01 And each ingredient in that list is an object again, with a name, an amount and a unit. Lists and records inside each other like this are called nested data.

01:12 Here, arrays and objects take turns, four levels deep. To go down an array level, you use square brackets and an index. To go down an object level, you use a dot and a key, or the key in square brackets, as with the pantry.

01:30 Now write the path to the onion, one step per level. Start at recipes. It’s an array, and the soup comes first, so square brackets and zero reach the soup’s record.

01:42 That record is an object, so a dot and ingredients reach its list of three. The list is an array again. Indexes count from zero, so the onion, second in the list, is at one.

01:54 Last, a dot and amount reach the number. Log it with a label, then save and reload the page. The Console shows the onion’s amount, one.

02:06 Paths work the other way too. Given a path, read it one step at a time against the sketch. This new log line keeps the first two steps, then takes index two and its unit.

02:19 Your turn: pause the video and predict what this line logs.

02:28 Save and reload, and it logs the letter L.

02:33 Index two is the third ingredient, the stock, and its unit is liters.

02:38 Watch out for a path that skips a level, going from recipes straight to the ingredients field. The array has no field by that name, so that step gives undefined, and the next step throws a TypeError.

02:51 Chrome’s message names the step after the undefined step, so the fix goes before ingredients. It’s the recipe’s index, zero.

03:01 As a rule, sketch the shape before you write a path. Then read the path from left to right, one level per step: square brackets and an index for an array, and for an object, a dot and a key or the key in brackets.

03:17 So far, you’ve reached one value at a time. Next, in Process Nested Lists and Grids, you’ll choose shapes for Sam’s data and loop over their levels.

Become a Member to join the conversation.