Nested Arrays and Objects, online
Six activities your students do on their own devices. See who's finished, and their scores.
Nested Arrays and Objects in one minute
What students need to know
The first bracket picks a row, the second picks a column. The bracket picks the object, then the dot reads its property.
This chains dot notation and bracket notation to reach the nested value. You can mix .property and [index] to reach deeply nested values.
Mistakes students make
- Missing the dot before pets - it should be obj.pets[0].
- Missing the dot before addr - it should be obj.addr.
- Arrays use square brackets [], not round brackets ().
- Object literals close with }, not ].
What students will learn in this module
Every question in Module 35
Quick questions
- Does person.pets[0] access an element nested inside an object?ANoBYes
- Can dot notation and bracket notation be chained together?AYesBNo
- Does grid[0][1] access an element in a 2D array?ANoBYes
- Is users.0.name valid syntax for accessing an array element?ANoBYes
- Does obj.addr.city access a property nested two levels deep?AYesBNo
Key terms
Students type the meaning of:
obj.pets[0]
Match the pairs
obj.pets[0]Match it to what it doesobj.addr.cityMatch it to what it doesAccesses an element in a 2D array using two indexesMatch it to what it doesAccesses a property on an object stored inside an arrayMatch it to what it doesusers.0.nameMatch it to what it doesobj.addr[city]Match it to what it doesFill in the blanks
console.log(person.pets_0]);
Hint: Type the symbol that opens an index access on the nested array.
console.log(obj.addr_city);
Hint: Type the symbol that connects addr to its nested property.
console.log(users[0]_name);
Hint: Type the symbol that connects the array element to its property.
console.log(data.scores[_]);
Hint: Type the index of the third element in scores.
console.log(grid[0]_1]);
Hint: Type the symbol that opens the second index access.
Spot the error
Question 1
Aconsole.log(objpets[0]);Blet obj = {};Cobj.pets = ["Rex", "Mia"];
Question 2
Aobjaddr = {city: "NYC"};Blet obj = {};Cconsole.log(obj.addr.city);
Question 3
Alet obj = {};Bobj.pets = ("Rex", "Mia");Cconsole.log(obj.pets[0]);
Question 4
Alet obj = {};Bobj.addr = {city: "NYC"];Cconsole.log(obj.addr.city);
Question 5
Alet obj = {};Bobj.pets = ["Rex", "Mia"];Cconsole.log(obj.pets.0);
