I have a list of json object, with the attributes: first name, last name, and gender. I have a fetch function as such:
buttonsend.addEventListener("click", function(){
fetch("http://uinames.com/api/?amount=25®ion=denmark&gender=female").then(
function(response){
return response.json();
}
).then(function(jsonData){
for(var i = 0; i <= jsonData.length; i ++){
console.log(JSON.stringify(jsonData[i]).gender);
}
//document.getElementById("json").innerHTML = JSON.stringify(jsonData);
console.log(2);
});
});
In my for loop how can I access each of the elements of the object, and display them in a table in my HTML code?
var data = JSON.parse(jsonData)and then do the loop and simplydata[i].theAttributeYouWant. Also you’re trying to use stringify which converts it to text and not to an object. Your loop is not working because you’re trying to iterate a text, not a list of objects.