Im JavaScript I have an array of objects each with an image url. I want to remove the objects where the image url is broken (returning a 404).
My code so far, where items is an array of objects:
function rmBadImageCheck (items){
console.log("doing image checks");
var x = items.length;
for (var i = 0; i < x; i++) {
var img = new Image()
img.src = items[i].image_url;
img.onerror = function(){
console.log("need to splice at: " + i)
items.splice(i, 1)
}
}
console.log(items);
return items;
}
But as the img.onerror is asyncronous the loop is finished by the time the splice gets called. How can I remove the images with errors from the array.