Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Objects Loop Through Objects Display an Array of Objects on the Page – One Solution

I had the impression that looping the html inside the for loop would work. But why doesn't it?

Here's my code:

/*
  Create an array of 'pet' objects.
  Each object should have the following properties: 
  name, type, breed, age, and photo
*/

const petDirectory = [
      {
        name: "Aussie",
        type: "Dog",
        breed:"Australian Shepherd" ,
        age: 3 ,
        photo: "img/aussie.jpg", 

      },
      {
        name: "Dachshund",
        type: "Dog" ,
        breed: "Dutchhound",
        age:4 ,
        photo: "img/dachshund.jpg", 
      },
      {
        name: "Golden",
        type: "Dog" ,
        breed: "Golden Retreiver",
        age: 2,
        photo: "img/golden.jpg" , 
      },
      {
        name: "Persian",
        type: "Cat",
        breed: "Persian cat",
        age: 3.5,
        photo: "img/persian.jpg", 
      },
      {
        name: "Pug",
        type: "Dog",
        breed: "Pug",
        age: 5 ,
        photo: "img/pug.jpg", 
      },
      {
        name: "Tabby",
        type: "Dog",
        breed: "Tabby",
        age: 4 ,
        photo: "img/tabby.jpg", 
      }
];

for ( let i = 0; i < petDirectory.length; i++ ) {
   let pet = petDirectory[i];
  document.querySelector('main').innerHTML = `
      <h2>${[i].name}</h2>
      <h3>${[i].type} | ${[i].breed}</h3>
      <p>Age: ${[i].age}</p>
      <img src="${[i].photo}" alt="${[i].breed}">
`;
}

1 Answer

Hi melis!

Two issues:

1) In your HTML string you have [i].name (and [i].type, etc.). It should be pet.name (etc.).

2) You should declare an html var (using let) before the loop and write to the var using += in the loop and then write to the innerHTML using the resulting html var after the loop. Otherwise, you just keep overwriting the innerHTML and you'll only see the last pet data.

Like this:

const petDirectory = [
      {
        name: "Aussie",
        type: "Dog",
        breed:"Australian Shepherd" ,
        age: 3 ,
        photo: "img/aussie.jpg", 

      },
      {
        name: "Dachshund",
        type: "Dog" ,
        breed: "Dutchhound",
        age:4 ,
        photo: "img/dachshund.jpg", 
      },
      {
        name: "Golden",
        type: "Dog" ,
        breed: "Golden Retreiver",
        age: 2,
        photo: "img/golden.jpg" , 
      },
      {
        name: "Persian",
        type: "Cat",
        breed: "Persian cat",
        age: 3.5,
        photo: "img/persian.jpg", 
      },
      {
        name: "Pug",
        type: "Dog",
        breed: "Pug",
        age: 5 ,
        photo: "img/pug.jpg", 
      },
      {
        name: "Tabby",
        type: "Dog",
        breed: "Tabby",
        age: 4 ,
        photo: "img/tabby.jpg", 
      }
];

let html = '';
for ( let i = 0; i < petDirectory.length; i++ ) {
  let pet = petDirectory[i];
  html += `
      <h2>${pet.name}</h2>
      <h3>${pet.type} | ${pet.breed}</h3>
      <p>Age: ${pet.age}</p>
      <img src="${pet.photo}" alt="${pet.breed}">
`;
}
document.querySelector('main').innerHTML = html;

I hope that helps.

Stay safe and happy coding!