I am new to JS and I have a problem and can't get the solution. I have written a backend with Java SE, Jersey and Jackson. Everything is fine. My REST endpoint is running. But I cannot figure how to reference that json array object. The "time" values are working correct. The array of "team" say always "undefined".
I want to fill a HTML table with those values. Here is the code snippet:
<script>
var dataExchange = []
$.ajax({
method:'GET',
url:'http://localhost:8080/api/calendar',
success:function(response){
dataExchange = response
buildTable(dataExchange)
console.log(dataExchange)
}
})
function buildTable(data){
var table = document.getElementById('eventTable')
for (var i = 0; i < data.length; i++){
var row = `<tr>
<td>${data[i].dateOfEvent}</td>
<td>${data[i].team.teamName}</td> <---Here is the problem
</tr>`
table.innerHTML += row
}
}
</script>
Thats the JSON from the backend:
[{"dateOfEvent":"2019-07-18T18:30","team":[{"teamName":"Salzburg"}]},{"dateOfEvent":"2019-07-18T18:30","team":[{"teamName":"Sturm"}]},{"dateOfEvent":"2019-10-23T09:45","team":[{"teamName":"Capitols"}]},{"dateOfEvent":"2019-10-23T09:45","team":[{"teamName":"KAC"}]}]
Please, can anyone explain what I am doing wrong here and how does this exactly work with JavaScript?
databefore using it in the function?console.log(dataExchange)output in the console? A string or an object?