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 trialmojoexmachina
4,702 PointsSolving this with a 'for in' loop?
I tried to solve this problem with a 'for in' loop by using bracket notation in combination with dot notation and was surprised to see that it works. Is this a valid way to solve this?
for ( let prop in pets) {
html += `
<h2>${ pets[prop].name }</h2>
<h3>${ pets[prop].type } | ${ pets[prop].breed }</h3>
<p>Age: ${ pets[prop].age }</p>
<img src="${ pets[prop].photo }" alt="${ pets[prop].breed }">
`;
}
1 Answer
Steven Parker
231,269 PointsThat's not an example of "bracket notation" for object properties, it's simple array indexing. The name "prop" is perhaps a slightly misleading name for the array index variable.
You've just skipped the step in the video where the "pet" variable is created to represent the current item (and to save needing the index multiple times).