title: How can I escape $.each loop with my data?
body: <p>I'm doing an Json call to retrieve an a list of locations with information details for each location. longitude and latitude are included in this info. </p>
  
  <p>I am using Google's distance matrix api to get the distance from coordinates provided as a query in the url. </p>
  
  <p>I thought the most logical way to do this would be to loop through the locations and compare searched and location coordinates per location. I am appending some of the info returned from the distance matrix calls onto my data array. </p>
  
  <p>This works fine except the data returned from the distance matrix is only usable within the return function for the distance matrix. I pass the wanted data from the distance matrix return function into a function outside the matrix return function but still inside the $.each loop. This seamed like a step towards solving my problem but I'm stuck.</p>
  
  <p>I need to use the original data but with the distance matrix data appended so that I can sort the items in the data by distance and output it. I don't think a sort function would work well inside of the $.each loop so I need to break out of the loop with the appended version of the data intact. at least I think. any help would be appreciated. </p>
  
  <pre><code>var ltlg = [];
  var sLat = parseFloat(35.2219971);
  var sLng = parseFloat(-101.83129689999998);
  var sLatLng = {lat: sLat, lng: sLng};
  
  var locLat;
  var locLng;
  var locLatLng = [];
  
  $.getJSON("file.json",function(data){
  
      if(sLat &amp;&amp; sLng) {
  
          $.each(data, function(x, y){
  
              locLat = data[x].Address.Location.Latitude;
              locLng = data[x].Address.Location.Longitude;
  
              //locLatLng.push({lat: locLat, lng: locLng});
  
              var service = new google.maps.DistanceMatrixService;
  
              service.getDistanceMatrix({
                  origins: [sLatLng],
                  destinations: [{lat: locLat, lng: locLng}],
                  travelMode: 'DRIVING',
                  unitSystem: google.maps.UnitSystem.IMPERIAL,
                  avoidHighways: false,
                  avoidTolls: false
              }, function(response, status) {
                  if (status !== 'OK') {          
                      console.log('status is ' + status);        
                  } else {
                      var distance = response.rows[0].elements[0].distance.value;
                      var miles = response.rows[0].elements[0].distance.text;
                      var time = response.rows[0].elements[0].duration.text;
  
                      addData(distance, miles, time);
                  }
  
              });
  
              function addData(distance, miles, time) {
                  data[x]["distance"] = distance;
                  data[x]["miles"] = miles;
                  data[x]["time"] = time;
              }
  
          });
  
          console.log(JSON.stringify(data));
  
      } else {
          console.log('no query');
      }
  
  });
  </code></pre>
  
  
  
  <p>I replaced the query variables with static lat lng for the snippet.</p>