I’m working with some images from the Flickr API using jQuery, and looping through the result set.
This works:
$.getJSON(apiCall, function(data) {
//LOOP THROUGH DATA
$.each(data.photos.photo, function(i, photo) {
// do stuff
});
});
but when I added a check to make sure that the photos object was populated, the length property is undefined.
$.getJSON(apiCall, function(data) {
console.log(data.photos.length); // returns undefined
if(data.photos.length >= 1){
//LOOP THROUGH DATA
$.each(data.photos.photo, function(i, photo) {
// do stuff
});
} else {
// do other stuff
}
});
What is the proper way to determine whether the photos object is populated?
EDIT: sample of json data returned:
{"photos":{"page":1, "pages":100172, "perpage":50, "total":"5008581", "photo":[{"id":"8077408556", "owner":"75972124@N08", "secret":"ede5a49ab5", "server":"8323", "farm":9, "title":"zara 2", "ispublic":1, "isfriend":0, "isfamily":0}. . .
Yes, it is possible to simply test if photos.total >= 1 but I’m curious about the length property.
Try
To get the length, you need to use
data.photos.photo