Often, on a callback that receives json, you may be tempted to do something such as:
if (data.image.length) {
// Group of images
for (var i = 0; i < data.image.length; i++) {
debug("Image " + data.image[i].imageUrl + ": " + data.image[i].statusText);
}
}
else {
// Just a single image
debug("Image " + data.image.imageUrl + ": " + data.image.statusText);
}
but it can be expressed more succinctly as:
if (!data.image.length) data.image = [data.image];
// Group of images
for (var i = 0; i < data.image.length; i++) {
debug("Image " + data.image[i].imageUrl + ": " + data.image[i].statusText);
}
No comments:
Post a Comment