Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

282
Vistas
I can't figure out how to display this data in a separate line in my html from JS
  document.querySelector("#enter").addEventListener("click", e => {
        e.preventDefault();
        var inputValue = searchApi.value; 
        fetch('https://wft-geo-db.p.rapidapi.com/v1/geo/cities/' +inputValue, options)
        .then(response => response.json())
        .then(response =>  {
            console.log(response);
            return response;
        })
        .then((data) => {

            var title = document.createElement("li")
            title.classList.add('list-item');
            title.innerText = JSON.stringify(data);
            var list = document.getElementById("result-list");
            list.appendChild(title);
        })
        .catch(err => console.error(err));
    
    });

HTML looks like this

<h2 class="justify-center flex">Results</h2>
<ul id="result-list">
  <li class="list-item "></li>

</ul>

Because I use JSON stringify it turns out as it did, but I want the data to be displayed one by one and not all in one line. I have tried forEach, and for loop but I don't think I did it correctly, so any help is appreciated. This is how it displays on the webpage I want it to display as it looks in the console log

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your data is not an array, it is a plain object-- as such, you cannot simply loop over it by index like you would an array or other iterable. If you want to get all key/value pair entries from this object into an array to iterate you can use Object.entries:

.then((data) => {
  const myData = data.data;

  Object.entries(myData).forEach(([key, val]) => {
    var title = document.createElement("li")
    title.classList.add("list-item");
    title.innerText = `${key}: ${val}`;
    var list = document.getElementById("result-list");
    list.appendChild(title);
  });
})

Note that this will probably come back in alphabetical order-- if you want a specific order, you would need to adjust your approach. You can also use the Object.entries approach for other solutions listed in your original question on this topic.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda