I am trying to remove a wine from a JSON wine list and I want to display an alert if the wine doesn't exist in the JSON file. However, the alert is popping up for every entry in the list. I am struggling to find a way to use an if statement before proceeding to the else block.
function deleteWine(){
var deleteInput = document.getElementById('deleteInput');
var deleteInputValue = deleteInput.value;
for(wine=0; wine<dataHent.wines.length; wine++){
if (dataHent.wines[wine].catalog == deleteInputValue){
dataHent.wines.splice(wine,1);
var request = new XMLHttpRequest();
request.open("POST","writeWine.php",false);
request.setRequestHeader('Content-type','application/x-www-form-urlencoded');
request.send("wines="+JSON.stringify(dataHent));
removeDiv();
highlightSelection();
deleteInput.value ='';
break;
}
else {
alert('This wine does not exist');
}
}
}