I find myself wandering in the land of possibilities and would greatly appreciate some direction. After spending 2-3 hours scouring through countless SO questions and documentation related to my current predicament, I still seem to be missing the mark.
Overview
The code snippet provided below is designed to work with an Object type (resources
) by extracting specific values and using them to calculate distance and duration using GoogleMaps Distance Matrix. The googleRequest()
function returns a promise with two values (distance and duration).
My goal is to retrieve these values within the for loop, perform pushToRows()
, and ultimately return an array called final_rows
.
Problem
Upon inspection, it appears that the final_rows
array displays UNDEFINED for both duration and distance keys in every row. This could be attributed to improper access of values from dist_dur
. Any assistance in resolving this matter would be greatly appreciated. Thank you.
Code
final_rows = []
function getDistTime(resources){
for (var i = 0; i < resources.data.length; i++) {
var origin1 = $("#citystate").val();
var destinationA = resources.data[i]['DEMOBILIZATION CITY'] + ',' + resources.data[i]['DEMOBILIZATION STATE'];
var dist_time_data = googleRequest(origin1, destinationA).then((values) => {
return values
})
pushToRows(resources.data[i], dist_time_data)
}
// console.log(final_rows)
}
function pushToRows(resources, dist_dur){
resources["DISTANCE_MI"] = dist_dur[0];
resources["ACTUAL_DUR_HR"] = dist_dur[1];
resources["FINANCE_DUR_HR"] = (dist_dur[0] / 45.0).toFixed(2)
final_rows.push(resources)
}