For in an AJAX array

Asked

Viewed 70 times

0

I receive data from an API with an array and parse it with pure js.

This array returns me more than one object that I’m currently only displaying in the browser’s.log console. I don’t know how to do a for in pure js. How do I make a for and then display the data of this array on the page with the separate objects. When I get a single one I do the following:

    if(xhr.status == 200){


      let curso = xhr.responseText;
      curso = JSON.parse(curso);

      let video = '';
      video += curso.video;
      document.querySelector('#video').innerHTML = video;

      let descricao ='';
      descricao += curso.descricao;
      document.querySelector('#curso-descricao').innerHTML = descricao;

    }

This way it displays the video and description data, but it is only with one object. How do I do this same action to list the objects in my frontend using pure js?

3 answers

0


I did it this way and it worked:

if(xhr.status == 200) {
  let aulas = xhr.responseText;
  aulas = JSON.parse(aulas);

  let x = document.querySelector('#videos');
  aulas.map(item => {
    x.innerHTML += '<a href="responde-exercicio.php?id=' + item.id + '"><span>' + item.video + '</span></a>';
  });
}

0

It can be done as follows:

for(var i=0; i <= curso.length; i++){
  //Faça o que quiser acessando os objetos do array pelo indice
  //utilizando o i como exemplo abaixo.

  console.log("video: " + curso[i].video);
  console.log("descricao : " + curso[i].descricao);
}

A hint is to always check for something in the array:

if(curso !== null && curso !== undefined && curso.length > 0 ){
//o for vai aqui
}
  • I did that and it didn’t work out.. It didn’t show me anything on the console..

  • if (xhr.status == 200) { for (var i = 0; i <= course.length; i++) { //Do whatever you want by accessing array objects by Indice ////using i as an example below. console.log("video: " + course[i].video); console.log("Description: " + course[i].Description); } }

  • Francis, did you come to put a break point and see if there’s anything in the variable course? with the if if it has no value in it it will not show up at all.

  • So I really don’t understand you well. I just need to take the data I’ve already taken before and do a for or a foreach to display others. I didn’t even have this variable course in my code.. I don’t understand your answer and if I knew so much, I wouldn’t be here in the forum...rs

  • Francis, with "variable course" was referring to the excerpt: Let stroke = xhr.responseText; course = JSON.parse(course); Anyway, try like this: if (xhr.status == 200) { Let curso = xhr.responseText; stroke = JSON.parse; Let video = '; for(var i=0; i<=course.length i++) { console.log("Video: " + course[i].video); console.log("Description: " + course[i].Description); //or Document.querySelector('#course-Description'). innerHTML += stroke[i]. Description; } }

  • So I tested it here, but even the console.log warnings don’t show up...

  • Oops... I saw a mistake here that I had made.. then I fixed and appeared the following error: class-by-course.js:32 Uncaught Typeerror: Cannot read Property 'video' of Undefined at Xmlhttprequest.<Anonymous> (class-by-course.js:32)

Show 2 more comments

0

You can use the foreach for this.

In this example we will use your variable video:

video.forEach(function(curso) {
    var retornoVideo = document.querySelector('#video');
    return retornoVideo.innerHTML += curso.video;
});

See if this works and tell me if it helped or not.

  • Hello, I did this way and nothing appeared no.. What can I do? if (xhr.status == 200) { video.foreach(Function (stroke) { var retornoVideo = Document.querySelector('#video'); Return retornoVideo.innerHTML += stroke.video; }); }

  • Publish your code to jsFiddle for me to analyze, please.

  • https://jsfiddle.net/francisVagnerDaLuz/o1hdgg8s/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.