There are times when retrieving building geolocation data from a service results in missing properties within the data.
Example of Good Data:
[{
"ModifiedOn": "2015-04-29 11:17:28.0",
"BuildingName": "Winterfell",
"Latitude": "52.900619",
"Longitude": "-1.434602",
"Region": "The North",
}, {
"ModifiedOn": "2015-04-29 11:17:28.0",
"BuildingName": "Water Gardens",
"Latitude": "51.818051",
"Longitude": "-.354871",
"Region": "Dorne"
}]
Example of Bad Data:
Missing Region or Building Name
{
"ModifiedOn": "2015-04-29 11:17:28.0",
"BuildingName": "Kings Landing",
"Latitude": "23.818051",
"Longitude": "-.154871",
}
Upon receiving the JSON response through an AJAX request, it is stored in an object named _regionAndBuildings.
The goal is to eliminate any bad data entries, so the following code was attempted:
console.log("Initial size of building data : " + _regionAndBuildings.length);
// Clean the JSON by setting objects to undefined
for (var i = 0; i < _regionAndBuildings.length; i++) {
if (_regionAndBuildings[i].BuildingName === undefined && _regionAndBuildings[i].Region === undefined) {
console.log("Deleting");
delete _regionAndBuildings[i];
}
}
console.log("Final size of building data after cleanup : " + _regionAndBuildings.length);
Output:
Initial size : 209
Deleted x 17
Final size : 209
The issue arises when trying to remove the bad elements from the JSON object using the provided code. Why is this happening?