I have been on this problem for some hours. The code work like these : someone does an image search, I fetch the answer from an API and I inject the images into a div to present the result. It can vary from 3 to 500 images, it's not fixed.
Except that I cannot manage to display only the right number of images. The problem is in the iteration or the way I present data, but after trying everything and anything I think I need help.
What I receive from the API comes in this form:
{"icon":{"0":"https://address/1587.svg","1":"https://address/3810.svg","2":"https://address/89090.svg","3":"https://address/89400.svg"}}
And here's my (simplified) code:
makeHTTPRequest('GET', url).then((resp) => {
var parsedResp = JSON.parse(resp);
var arrayicon = Object.keys(parsedResp).map(function(key) {
return parsedResp[key]
});
injectIcons(arrayicon)
};
injectIcons = (icons) => {
let htmlString = '';
for (var i = 0; i < icons.length; i++) {
const IconDiv = `src="${icons[i]}"`;
htmlString = htmlString + IconDiv
}
document.getElementById('results').innerHTML = htmlString
};