Here is the JavaScript code I am working on (the "data" variable is retrieved from a json call):
if (data.projectReports.length) {
for (var i in data.projectReports){
var report = data.projectReports[i];
$('#reports').append(
'<div class="report-description">' +
'<h2>' + report.header + '</h2>' +
'<p>' + report.text + '</p>' +
'</div>' +
'<ul class=\"report-moreinfo\">' +
// Loop through the "persons" object here.
'</ul>'
);
}
} else
. . .
This is my JSON data:
{
"projectReports":[
{
"header":"Headline",
"text":"Description of item",
"estimate":10,
"actual":7,
"persons":{
"Robert":5,
"Erik":10,
"Juan":3
}
}
]
}
I'm still learning about JSON and encountered some challenges when attempting to loop through the "persons" object within each report. After researching various solutions, I decided to seek assistance by providing all necessary details here.
My goal is to iterate over report.persons
where the comment is placed in my JavaScript code.
Prior solutions were straightforward when referencing specific keys like "header" or "text", but this time I only have key-value pairs. How can I achieve this?
<li><p> persons.key </p><p> persons.value </p></li>
I recognize that I will need another nested for
loop, but I lack the expertise to construct it on my own.