Ajax-loader-large-white
Introduction to Objects I
Loop the loop

Arrays filled with objects will work just like arrays filled with numbers and strings.

In the same way we may loop through an array of numbers to print them out or calculate a sum, we can loop through an array of objects and access properties or methods.

Instructions
  1. Write a person constructor called Person that has two properties (name and age).
  2. Create an empty array called family.
  3. There will be four objects in the array. Using your Person constructor, create the four objects and put them in the array. The order of the objects are:

    • "alice" who is 40
    • "bob" who is 42
    • "michelle" who is 8
    • "timmy" who is 6
  4. Create a for-loop that loops through the family array and prints out the name property for each family member in order of creation.

?
Stuck? Get a hint! Hint
Get live, fast support from Codecademy Advisors when you get stuck. Learn more.

If you need a quick brush-up on how for-loops work, review the exercises on loops.

Accessing the name property in the array and then using this in the for-loop is the trickiest part. Remember that we use dot notation to get the value of a property from an object:

objectName.propertyName;

And we can use the same dot notation to get the value of a property for one particular object in an array.

arrayName[2].propertyName;

This will get the third object in the array, and then find the value associated with the property.

Note that the objects must be in the order specified in the instructions!

script.js