I’m trying to fetch an API with Javascript, and it’s working, but I can’t figure out how to make readable. This is my code:
And this is the output:
I’ve done this with ES6 several times, but I don’t understand how to do it in vanilla. Newbie question I guess, but would appreciate some help.
Thanks in advance!
I tried to add JSON.stringify(data, null, 2); but it didn’t help. I also tried to just fetch some of the endpoints by adding the name of the endpoint after data, but then nothing shows.
2
Answers
The API response data is assumed to be an array of objects. The data.forEach() method is used to iterate over each object in the array. Within the loop, the headline and paragraph properties are extracted from each object.
A new div element is created for each item, and its content is set using template literals to include the extracted data. This creates a structured HTML markup with the headline as an h2 element and the paragraph as a p element.
Finally, the newly created div element is appended to the dataContainer element on your webpage, which will display each item separately.
You need to parse and extract the bits.
For example like this