Object-ception
Lesson objectives
After this lesson students will be able to:
Use an array inside an object
Iterate over an array that is within an object
Use an object within an object
Use an object within an object within an object
Use an array within an object within an object within an object
Use an array of objects
Use variables to store a key
Loop over an object
Use an array inside an object
Let's model an adventurer who has belongings (a list)
Access all values in the player.belongings array:
Access a specific item in the belongings array:
Iterate over an array that is within an object
Activity
Write an object for an
adventurer
and give the adventurer some belongingsAccess the belongings array and print the last element to the console
Use
.push()
to add a "Vogue Magazine" to the belongings arrayWrite a for loop that prints each element in the belongings array to the console
Use an object within an object
Our adventurer now has a companion! Our companion, a bat, is an object with its own properties.
Add the companion
object to the adventurer
object:
Access the companion object:
=> { name: "Velma", type: "Bat" }
Access the companion's name:
=> "Velma"
Access the companion's type:
=> "Bat"
Activity (3 min)
Write the companion object into the adventurer object
Print just the companion's name to the console
Change the companion's name "Velma" to "Susan"
Console.log to check that the name was changed
Add another object to the
adventurer
object calledcompanion2
.companion2
should have a name and a type "Insect"
Use an object within an object within an object
Velma the bat also has a companion, a magical parasite called Tim.
Let's add Tim to our data:
What would you write to:
console.log Tim's type
Use an array within an object within an object within an object
Tim has a bag of holding and can carry an infinite number of belongings.
Let's add an array of belongings to Tim:
What would be the code to access:
console.log "health insurance"
Use an array of objects
A common pattern you will start to see everywhere (especially in Unit 2 and onwards) is an array of objects.
An array of objects can look like this:
These objects have no names, they are just anonymous objects packed into an array.
You could reference them with indexes as usual:
You could reference the properties by first asking for the index, then the property:
You could loop over the array and just print all of the titles:
Use variables to store a key
=> 'Slimer'
Loop over an object
There are two way to loop over objects. You can use either way. Both ways involve looping over the object's keys.
Let's say we have a movie object:
for ... in
loop
for ... in
loopPrint each key:
=>
title
director
year
Print each value:
To do this, use the key as a variable within the square brackets.
=>
L'Avventura
Michelangelo Antonioni
1960
Object.keys()
Object.keys()
Object.keys() will return an array of keys
=> [ 'title', 'director', 'year' ]
To print the values, use the key as a variable within square brackets.
Activity
Given the following movie:
Use a for..in loop to print all the keys of the movie object
Use a for..in loop to print all the values of the movie object
Advanced
With the following array of movie objects:
Create an empty object and assign it to a moviesObj
variable. Leverage the fact that Keys are unique in an object. Loop over the movies array and add the movie titles as keys in the moviesObj
object. This will consequently omit the duplicate movie. At the same time, give each movie key the value of an object containing the director and year. The final object should look like this:
Tip: You might want to use this technique for the hardest part of tonight's homework.
Use Object.keys loop to print all the values of the movie object.
This lesson was adapted from SEI-MAE
Last updated