HTML and CSS Reference
In-Depth Information
The last line of this code outputs our poster. We display another message to the user saying
that we've found a result, and then proceed to show the image.
Let's spend a moment figuring out how we got to the poster images using the line
json[0].posters[0].image.url
.
The reason we use
json[0]
is that — since we want to display only one poster, and knowing
how relevant TMDb's results are — we can gamble on the first result. We then access the
posters
array like so:
json[0].posters[0]
. Chrome even tells us that
posters
is an array,
so we know what we're dealing with. Again, we access the first value of the array, having faith
that it will be most relevant. It then tells us that
image
is an object, so we can access it like so:
json[0].posters[0].image
. By expanding our object further, we see that
image
contains a
property named
url
. Jackpot! This contains a direct image link, which we can use in the
src
attribute of our image element.
}
else
{
//If nothing is found, I attempt humor by displaying a Goonies poster and
confirming that their search returned no results.