Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda