Learning objective: Read and update nested objects and arrays, follow a path step by step, loop over nested collections, and print nested data clearly.
A weather API returns nested JSON: a location object and a list of hourly forecasts, each with nested temperature data. The app reads exactly the paths it needs and checks the shape first.
4. What is the best first step when a deep path throws?
Add try/catch everywhere
Log each step of the path to find the first undefined
Use JSON.stringify
Rename the properties
5. What is printed?
console.log(typeof JSON.stringify({ a: [1] }));
string
object
undefined
number
6. Many screens read order.customer.address.city. What helps when the data format changes?
Copy the path everywhere
One accessor function cityOf(order)
Global variables
Nothing
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the path: Prints Pear; No error
Shipping label from nested data: Prints the label; Not hard-coded
Course report: Prints the report; courseSummary works alone
ex-038-01: output matches the documented expected output
ex-038-02: output matches the documented expected output
ex-038-03: output matches the documented expected output
ex-038-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-08-objects-and-structured-data/lesson-038-nested-objects-and-arrays/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
Real data nests objects and arrays.
Follow paths one step at a time with . and [ ].
Log each step to find where a path breaks.
Use accessor functions for paths used in many places.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationnested objects and arrays
Context: I am learning nested objects and arrays in JavaScript.
Task: Explain how to read paths through nested objects and arrays with an order example.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionnested objects and arrays drill
Context: I have practised nested objects and arrays.
Task: Give me six nested data structures and ask me to write the path to a value.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingnested objects and arrays bug
Context: My code throws Cannot read properties of undefined on a long path.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationnested objects and arrays practice
Context: I finished the nested objects and arrays exercises.
Task: Create three exercises that summarise nested API-style data.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewnested objects and arrays review
Context: Here is my code that uses nested objects and arrays.
Task: Review how my code reads deeply nested data and suggest accessor functions.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.