I created this function to calculated distances I want to measure:
distanceToResponder: function() {
var distance = 0;
var incidentLocation = this.createCoordinate(this.currentIncident["latitude"], this.currentIncident["longitude"]);
this.responders.forEach(function(responder) {
var responderLocation = this.createCoordinate(responder.location.latitude, responder.location.longitude);
distance = this.calculateDistanceBetweenLocations(incidentLocation, responderLocation);
}.bind(this));
return distance;
}
for some reason which I do not see, the value for distance is only returned once. So if I throw 2 locations into the forEach function, and I use console.log within the loop, I see everything is calculated correctly. But if I return the value for distance, only one value is returned.
How do I return the value correctly?
I am using VueJS where 'distanceToResponder' is a computed property to fill a table:
<tr v-for="responder in responders">
<td>{{ responder.userReference }}</td>
<td>{{ distanceToResponder(responder) }}</td>
<td><button type="button" class="btn btn-danger" @click="createDispatch(responder)">Alarm</button></td>
</tr>
so the function should return the distance for each responder. How do I need to adjust my function then?
distance, and you can return from a function just once, anyway. You may be interested in theArray.mapmethodreturn distance;will return whatever the value ofdistanceis at that moment. All you are doing is overwriting the value insideforEach.