0

at the beginning I want to say that I checked a lot of solutions but none of it was straight accurate to my problem. I wonder if it's possible to fetch data from JSON (name, surname and id). In every of each list will be other data. The issue is length of lists will be as much as JSON's length so it has to be automatically generated, but unfortunately in promises, you are not allowed to make functions so what is the solution for that problem?

const nameList = document.querySelector('.name-list')
const surnameList = document.querySelector('.surname-list')
const idList = document.querySelector('.id-list')


fetch('http://www.json-generator.com/api/json/get/cuSKqtKmgi?indent=2')
    .then(response => response.json())
    .then(data => {
        console.log(data);
    });
body{
    display: flex;
    justify-content: space-around;
}
<body>
   <ul class="name-list">
    </ul>
    <ul class="surname-list">
    </ul>
    <ul class="id-list">
    </ul>
    <ul class="example">
        <li>Example1</li>
        <li>Example2</li>
        <li>Example3</li>
        <li>Example4</li>
        <li>Example5</li>
    </ul>
</body>

That is only an exercise but will help me understand fetch more. If something is unclear feel free to ask :)

2
  • 1
    Not sure what you mean with "but unfortunately in promises, you are not allowed to make functions". Besides that, you can iterate over the data (right where you console.log) and insert items in the 3 lists. Commented Mar 11, 2021 at 22:25
  • @GabrielePetrioli I had Uncaught (in promise) TypeError: can't access property "forEach", data.person.name is undefined error that's why I thought you're not allowed to do that, thanks :) Commented Mar 11, 2021 at 22:31

2 Answers 2

1

Maybe it is simple like:

let names = []
let surnames = []
let ids = []

fetch('http://www.json-generator.com/api/json/get/cuSKqtKmgi?indent=2')
.then(response => response.json())
.then(data => {        
    data.person.forEach(item => {
      names.push(item.name)
      surnames.push(item.surname)
      ids.push(item.id)
    })
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your solution, it really helps :)
0

You can use .forEach as follows:

const nameList = document.querySelector('.name-list')
const surnameList = document.querySelector('.surname-list')
const idList = document.querySelector('.id-list')

fetch('http://www.json-generator.com/api/json/get/cuSKqtKmgi?indent=2')
  .then(response => response.json())
  .then(data => {
    const { person = [] } = data;
    person.forEach(({ name, surrname, id }) => {
      nameList.innerHTML += `<li>${name}</li>`;
      surnameList.innerHTML += `<li>${surrname}</li>`;
      idList.innerHTML += `<li>${id}</li>`;
    });
  });
body{ display: flex; justify-content: space-around; }
<ul class="name-list"></ul>
<ul class="surname-list"></ul>
<ul class="id-list"></ul>

2 Comments

This is exactly what I wanted but can it be done in a different way instead of using innerHTML?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.