07-Images
In this module, we'll add the code for pulling off thumbnails from the JSON data.
The Code
Be meticulous as you do this next one. We've added steps for additional code. Be sure that you are paying attention to where things go. Then, we'll break the code down at the end:
Analysis
We add an
img
variable that will create an image element.We use a conditional to check the JSON for data. There is a multimedia property in the JSON. You should go look for it in the
json
. If that has anything in it (if the length is greater than 0), then, we'll do some stuff in the next steps.If there is an object, we want to concatenate a string with the url for the
html
src
value. We will add the first item in the multimedia array:current.multimedia[0].url
. That is all confusing, so it helps here to think about a regular old image tag:We are building that for each result item.
We need an
alt
if something should happen that the image isn't available. We set it to the value of the headline.We append the image to the article element for each of our items.
Run the App
You should see images when you run a search result:
Last updated