I have a js object like this
var routes = [
{lat: 12.44, lng: 74.50},
{lat: 12.54, lng: 74.60},
{lat: 12.64, lng: 74.70},
...
];
I want to calculate distance between 2 points from the routes array to create a new array with lat,lng and distance from previous point. Using recursive function I can do that in sequential order, so until the http request is not resolved I do not call the next points. here is the recursive code
var finalData = [];
function getData(points, i) {
if(points.length >= i+1) {
var p1 = points[i];
var p2 = points[i+1];
var url = 'http://to/web/app/origin=' + p1.lat + ',' + p1.lng + '&destination=' + p2.lat + ',' + p2.lng;
http.get(url, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var d = JSON.parse(data);
finalData.push({
lat: p2.lat,
lng: p2.lng,
distance: d.dis
});
getData(points, i+1);
});
});
}
}
getData(routes, 0);
Being new to Reactive Programming, not able to think and find out how can I achieve the same using Rxjs library? I mean some small declarative code.