I have a script that retrieves an external JSON. It then checks if a sub-array is empty, and if it is, it does something and moves on. If the sub-array is not empty, it will check starting from 0 each element in the array until it detects that it has finished. Then, it will do something and continue. This is the code that I have:
var alertCheckNumber = 0
function checkAlert () {
// if there are no alerts
if (data?.alerts===undefined) {
document.getElementById('allAlerts').style.display = "none";
return;
}
// proceed to check the number of alerts
if (data.alerts[alertCheckNumber]?.event===undefined) {
finalAlertNumber = alertCheckNumber;
var row = [ 0, 1, 2, 3, 4, 5, 6];
for (var i = 0; i<row.length; i++) {
if (row[i] >= finalAlertNumber) {
document.getElementById(`DIValertdescription${row[i]}`).style.display = "none";
document.getElementById(`DIValertlocation${row[i]}`).style.display = "none";
document.getElementById(`DIValertname${row[i]}`).style.display = "none";
}
}
return;
} else {
console.log(alertCheckNumber);
printAlertData(alertCheckNumber);
alertCheckNumber++
checkAlert();
}
}
However, the issue in the code is where I have data?.alerts===undefined
. This is supposed to check if data.alerts
is empty. This works fine in modern browsers, but when I run it in an older browser, like Chrome in Windows 7, I get this error:
Uncaught SyntaxError: Unexpected token '.'
pointing to the line of code I highlighted above.
How might I be able to fix this, so that it is backwards compatible?