Получить значения JSON численно в javascript

#javascript #json

Вопрос:

Я хотел бы, чтобы мой Javascript вызывал мой JSON (показано ниже), чтобы я мог получить к нему доступ в цикле while, например: v[0].название;

В принципе, я хочу знать, как перебирать каждое значение снизу вверх и вставлять его в свой html.

   xhttp.onreadystatechange = function() {
    if (this.readyState == 4 amp;amp; 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();
 

Вот мой 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"
    }

}
 

Комментарии:

1. Итак, ваш вопрос заключается в том, как выполнить цикл через объект?

2. Мне кажется, это серьезный вопрос @Prana

3. Смотрите мой ответ ниже. Причиной снижения голосов может быть название вашего вопроса. Это не имеет отношения к вашему вопросу

Ответ №1:

Поскольку вам нужно выполнить цикл по объекту, вы можете Object.keys преобразовать его в arrray, а затем использовать методы итерации массива.

 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
})
 

Комментарии:

1. Привет! Итак, как бы я повторил каждый «элемент», он же объект в JSON? Должен ли я создать новый вопрос, чтобы ответить на этот вопрос? @Прана