I have an object I want to pass on to functions and manipulate called spots
My setDistances
function does just that.
After the function I'll console.log
the manipulated value like this console.log('DistanceFrom Set. ' + distSetSpots[0].distanceFrom);
& it'll appear that the value hasnt been changed.
Whats weird is if i console.log
the entire object like this console.log(distSetSpots);
& transverse through it via chrome dev tools instead of programactically, it'll in fact be show the manipulated value.
This has left me pretty confused.. Can anyone explain whats happening here? Full relevant code below:
// Get a copy of the Spots data in spots.json
this.sds.fetchData( (spots) => {
// Get distance inbetween each spot & the user's location
this.lcs.setDistances(spots, (distSetSpots) => {
console.log(distSetSpots);
console.log('DistanceFrom Set. ' + distSetSpots[0].distanceFrom);
// 'Filter' spots (set 'pts' to null) that exceed max distance
this.filterFarSpots(distSetSpots, (filteredSpots) => {
// Find matched target species in spots & assign pts
this.findTargetSpecies(filteredSpots, (prefTargSpots) => {
});
});
});
}
);
setDistances function:
setDistances(spots, callback) {
const posRef = this.posObject;
for (let i = 0; i < spots.length; i++) {
const origin = new google.maps.LatLng(posRef.pos.coords.latitude, posRef.pos.coords.longitude);
const destination = new google.maps.LatLng(spots[i].coords[0], spots[i].coords[1]);
const service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [origin],
destinations: [destination],
travelMode: 'DRIVING',
}, (resp, status) => {
spots[i].distanceFrom = (resp.rows[0].elements[0].distance.value / 1000).toFixed(1);
});
}
callback(spots);
}