I have a filter that looks over objects in an array to see if the distance of the latitude and longitude of the object are within the user's selected range. However, the problem is that some of the objects do not have latitude and longitude attributes at all, and this breaks the filter. Is there a way to "skip" over the object if it does not have lat and long? or if its lat and long are 0?
Code:
function distanceFromFunc(distanceFrom) {
var m;
var n;
var positionContainer = [];
var docLat;
var docLon;
var rowIdFromObj;
var rowObj;
var rowsArray;
if (distanceFrom === "1") {
vm.selectedRadius = 1609.344 //1 mile
} else if (distanceFrom === "2") {
vm.selectedRadius = 3218.688 //2 miles
} else if (distanceFrom === "5") {
vm.selectedRadius = 8046.720 //5 miles
} else if (distanceFrom === "10") {
vm.selectedRadius = 16093.440 //10 miles
} else if (distanceFrom === "20") {
vm.selectedRadius = 32186.880 //20 miles
} else if (distanceFrom === "50") {
vm.selectedRadius = 80467.200 //50 miles
} else if (distanceFrom === "999999") {
vm.selectedRadius = 0
};
//THE PROBLEM HERE IS THAT WHEN IT HITS A RECORD WITHOUT LAT AND LONG
//THE FUNCTION BREAKS
function filterByDist(value) {
console.log("VALUE IS ------>" + JSON.stringify(value));
var dist = distance(myLat, myLon, value.locations[0].Lat, value.locations[0].Long);
if (dist <= distanceFrom) {
console.log("the dist is: " + dist);
return value
};
};
var digestedArray = originalData2.filter(filterByDist)
$scope.locatorGridData = digestedArray;
}; //end func
I beleive it breaks on this line: var dist = distance(myLat, myLon, value.locations[0].Lat, value.locations[0].Long);