skip to Main Content

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:

JS code

And this is the output:

How it looks like

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


  1. function fetchData() {
      fetch(URL)
        .then(response => response.json())
        .then(data => {
          const dataContainer = document.getElementById('dataContainer');
          // Extracting and displaying specific data
          data.forEach(item => {
            const headline = item.headline;
            const paragraph = item.paragraph;
            const itemElement = document.createElement('div');
            itemElement.innerHTML = `<h2>${headline}</h2><p>${paragraph}</p>`;
            dataContainer.appendChild(itemElement);
          });
        })
        .catch(error => {
          console.error('Error:', error);
        });
    }
    
    fetchData();
    

    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.

    Login or Signup to reply.
  2. You need to parse and extract the bits.

    For example like this

    const data = [{"vignette":"","order":"1","imageUrl":"","headlinePrefix":"JUST NU: ","headline":"Polisanmäler — timmar innan match","paragraphPrefix":"","paragraph":"AIK agerar efter nya avgångskrave t"},{"vignette":"#v-826f4b23-1df4-42c4-b20f-75 1aaa520117","order":"2","imageUrl":"https://static.cdn-expressen.se/images/8f/d7/8fd7fe0e7cf3 4cd9aaef1d47c34369e1/16x6/1280.png","head linePrefix":"","headline":"Elias sköts ihjäl i Farsta: "Han var bara ett barn"","paragraphPrefix":"","paragraph":"15-åringens familj fick tvätta bort hans blod. "Bråkade inte med någon""}, {"vignette":"","order":"3","imageUrl":"https://stati c.cdn-expressen.se/images/54/b6/54b6560a8d ef4b889b4d6ab12652be80/16x6/[email protected] g","headlinePrefix":"","headline":"Fattar ingenting efter fruns sms - mitt i tv-sändningen","para graphPrefix":"","paragraph":"Alexander Axén om orden som skapade panik: "Skrev att hon slåss""}]
    
    
    document.getElementById('dataContainer').innerHTML = data
      .map(item => `<h2><b>${item.headlinePrefix || ""}</b> ${item.headline}</h2>
    <p>${item.paragraph}</p>`).join('<hr/>')
    <div id="dataContainer"></div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search