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

101
Vistas
Loop through a JSON object and display items as unordered list

i have a fetch that return a json object like this:

{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "name",
"instant_invite": "link",
"channels": [
    {
        "id": "xxxxxxxxxxxxxxxxxxx2",
        "name": "Altri Giochi",
        "position": 8
    },
    {
        "id": "xxxxxxxxxxxxxxxxxxx2",
        "name": "Ascolto Musica",
        "position": 11
    },
],
"members": [
    {
        "id": "0",
        "username": "xxxxxxxxxxxxxx",
        "discriminator": "0000",
        "avatar": null,
        "status": "idle",
        "avatar_url": "https://url"
    },
],
"presence_count": 3

}

now i'm trying to map and display all these items to make an unordered list for my website, but i can't map this object with this.myResponse.map...i get an error "map is not a function".

i'm definitely lost here and i need some help to make an unordered list that contain all members name, avatar and other stuff.

can someone help me on this in vanilla js?

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

0

You can loop through the object using Object.keys(). Here's an example

let data = {
  "id": "xxxxxxxxxxxxxxxxxxx2",
  "name": "name",
  "instant_invite": "link",
  "channels": [{
      "id": "xxxxxxxxxxxxxxxxxxx2",
      "name": "Altri Giochi",
      "position": 8
    },
    {
      "id": "xxxxxxxxxxxxxxxxxxx2",
      "name": "Ascolto Musica",
      "position": 11
    },
  ],
  "members": [{
    "id": "0",
    "username": "xxxxxxxxxxxxxx",
    "discriminator": "0000",
    "avatar": null,
    "status": "idle",
    "avatar_url": "https://url"
  },
  {
    "id": "1",
    "username": "yyyyyyyyyyyyy",
    "discriminator": "0000",
    "avatar": null,
    "status": "idle",
    "avatar_url": "https://url"
  },],
  "presence_count": 3
}

data.members.forEach(m => {
  // start the UL
  let ul = "<ul class='member'>";
  // loop through using Object.keys(m) or preset array of keys
  ['username','avatar'].forEach(md => {
    ul += `<li>${md}: ${m[md]}</li>`;
  })
  ul += "</ul>";
  document.querySelector('#members').innerHTML += ul;
})
<div id='members'></div>

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