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?
I did that and it didn’t work out.. It didn’t show me anything on the console..
– Francis Vagner da Luz
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 Vagner da Luz
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.
– Raphael Ruedo
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 Vagner da Luz
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; } }
– Raphael Ruedo
So I tested it here, but even the console.log warnings don’t show up...
– Francis Vagner da Luz
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)
– Francis Vagner da Luz