0

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);

3 Answers 3

1

Should be able to just modify the offending line to be:

var dist;
if (value.locations && value.locations.length &&
    value.locations[0].Lat && value.locations[0].Long){
    dist = distance(myLat, myLon, value.locations[0].Lat, value.locations[0].Long);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just a little bit of checking before you make the distance method call:

function filterByDist(value) {
  if (value.locations[0].Lat && value.locations[0].Long) {
    var dist = distance(myLat, myLon, value.locations[0].Lat, value.locations[0].Long);
    if (dist <= distanceFrom) {
      return value
    }
  }
}

That assumes that the error does occur where you think, ofc! But it does appear plausible that it would error there.

Comments

1

The following code checks if:

  • value and value.locations are defined

  • Lat and Long exists

The distance is only calculated if these conditions are satisfied.

if(value && value.locations.length && value.locations[0].Lat && value.locations[0].Long) {
     // Calculate distance
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.