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

213
Visualizações
Get JSON values numerically in javascript

I would like to have my Javascript call my JSON (shown below) so that I can access it in a while loop, like this: v[0].title;

Basically, I want to know how to loop through each value from bottom to top and insert it into my html.

  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var vt = JSON.parse(this.responseText);
                while (document.getElementsByClassName("box-head") != vt.sux27.title) {
                
                                
                                document.getElementsByClassName("box-href")[i].href = vt.sux27.link;
                                document.getElementsByClassName("box-img")[i].src = vt.sux27.img;
                                document.getElementsByClassName("box-cat")[i].innerHTML = vt.sux27.categories[1];
                                document.getElementsByClassName("box-head")[i].innerHTML = vt.sux27.title;
                                document.getElementsByClassName("box-desc")[i].innerHTML = vt.sux27.description;                                
                                document.getElementsByClassName("box-likes")[i].innerHTML = vt.sux27.rating;
                                document.getElementsByClassName("box-author")[i].innerHTML = vt.sux27.author_name;
                                document.getElementsByClassName("box-author")[i].href = vt.sux27.author_link;
                        i++;
                        
                }               
        }
  };
  xhttp.open("GET", "https://www.global.cf/website-frontend/articles/articles.json", true);
  xhttp.send();

Here is my json:

{
    "sux27": {
        "img":"https://placecorgi.com/320/240", "categories" : ["#nature", "#pets"], "title" : "Best corgis to get", "description" : "This article  contains a cute corgi These dogs are extremely hardy, and will kill without hesistation...", "link" : "https://www.global.cf", "rating" : 27, "author_name" : "Pex co", "author_link" : "www.pex.com"
    },
    "cat": {
        "img":"http://placekitten.com/320/240", "categories" : ["#pets", "#gross"], "title" : "Cats up the wazzou", "description" : "Do you love dirty yet found cute by some, yet spread desease? Get a cat! This is totaly for u", "link" : "pets.com/pets", "rating" : 0, "author_name" : "Pets.com", "author_link" : "pets.com"
    }

}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since you need to loop through a object, you can use Object.keys to convert it into arrray and then you can use array iteration methods.

Object.keys(vt).forEach( (item, id) => {
   // here item will equal to each key in the object
   document.getElementsByClassName("box-href")[id].href = vt[item].link
   // you can continue doing this for your rest of the elements
})
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