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 AJAX Basics (retiring) AJAX and APIs Displaying the Photos

Jason S
Jason S
16,247 Points

data vs photo

I am a little confused about the reasoning behind using photo.link rather than data.items.link does it not get the same thing?

1 Answer

Jess Hines
Jess Hines
5,411 Points

It's not the same thing, and actually, data.items.link wouldn't return anything, since items is an array, hence you would need to access its properties with an index, e.g data.items[0].link.

data.items is the array you're iterating over, it's what is passed to the $.each() function, so within that, you use the value passed in to the anonymous function. In other words, jQuery takes your array, data.items, and passes each one individually to your anonymous function as "photo" and so within that function, you access any property on the photo with dot notation.

As mentioned in the video, you can use a plain for() loop if you wish, like so:

for (var i = 0; i < data.items.length; i++) {
  var link = data.items[i].link;
  ...
}

but the each() function just provides a shorter way to do that, without having to worry about the index.