I am attempting to dynamically generate some HTML lists based on JSON data fetched from a PHP script. The returned data is in the following format:
SAMPLE JSON DATA:
[{"animal":"Dog","breed":"German Shepard","image":"germanshepard.png","ownerID":"885","Status":"Check In"},
{"animal":"Dog","breed":"Poodle","image":"poodle.png","ownerID":"131","Status":"Checked In"},
{"animal":"Dog","breed":"Labrador","image":"labrador.png","ownerID":"6","Status":"Waiting Consultation"},
{"animal":"Bird","breed":"Parrot","image":"parrot.png","ownerID":"22","Status":"Checked In"},
{"animal":"Bird","breed":"Finch","image":"finch.png","ownerID":"31","Status":"Checked In"},
{"animal":"Cat","breed":"Persian","image":"persian.png","ownerID":"19","Status":"Waiting Consultation"},
{"animal":"Cat","breed":"Sphynx","image":" sphynx.png","ownerID":"44","Status":"Allocated Kennel"}]
I'm trying to create a div for each unique animal, ie. 1 div per unique animal. eg.
<div class='dog'></div>
Within each div I am trying to append an ul for each unique breed eg.
<ul class='Poodle'></ul>
with the final result looking like so:
ul {
background: #fff;
color: #000;
border: 1 px thin solid #000;
}
/* repeat for all status classes */
.Checked In {
color: 'green';
}
<section id='container'>
<div class='Dog'>
<ul class='German Shepard'>
<li><img src='germanshepard.png'/></li>
<li class='${ownerID}'>885</li>
<li class='${Status}'>Check In</li>
</ul>
<ul class='Poodle'>
<li><img src='poodle.png'/></li>
<li class='${ownerID}'>131</li>
<li class='${Status}'>Checked In</li>
</ul>
</div>
<div class='Bird'>
<ul class='Parrot'>
<li><img src='parrot.png'/></li>
<li class='${ownerID}'>22</li>
<li class='${Status}'>Checked In</li>
</ul>
<ul class='Finch'>
<li><img src='finch.png'/></li>
<li class='${ownerID}'>31</li>
<li class='${Status}'>Checked In</li>
</ul>
</div>
</section>
I'm returning the data like so:
function getAnimalData() {
let url = "php/getAnimalData.php";
fetch(url)
.then((res) => res.json())
.then((data) => {
for (const [key, value] of Object.entries(data)) {
byID('container').insertAdjacentHTML('afterend',
`<div class="${value['animal']}">
<ul class="${value['breed']}">
<li class="${value['image']}"></li>
<li class="${value['owner']}"></li>
<li class="${value['status']}"></li>
</ul>
</div>`);
}
}).catch(err => {
return Promise.reject(err.message);
});
}
As you can tell this doesn't filter unique animal and creates a div for each animal with each breed beneath it. I have attempted a few solutions on stackoverflow (1 for example) - most of which involve creating an array of distinct animal names then looping through that to create the multiple ul elements and then append the li by again looping over an array of unique breeds and appending appropriately. Unfortunately, I haven't found a solution that works and anything that comes close to working is using jQuery, knockout, etc (i'd very much like not to use jQuery).
I'm open to suggestions on how to achieve this using Vanilla JavaScript - or is this a case of going back to the drawing board and formatting the JSON data differently.