My objective is to locate the closest coordinate and arrange the cities that are nearest. In the console log, I want the output to appear like this: 8 city, 63.50590523722971; 5 city, 76.32168761236873. The coordinates seem correct, but I am struggling with how to assign the "city" attribute to result[i] and retrieve it.
var locationcity = [
{"city":"1 City","value":"61,90"},
{"city":"2 City","value":"34,97"},
{"city":"3 City","value":"21,63"},
{"city":"4 City","value":"19,84"},
{"city":"5 City","value":"0,81"},
{"city":"6 City","value":"6,76"},
{"city":"7 City","value":"43,64"},
{"city":"8 City","value":"18,64"},
{"city":"9 City","value":"10,61"},
]
function findClosest(locationcity){
var result = [];
for(var i=0; i < locationcity.length; i++){
for(var j = 1; j < locationcity.length-1 ; j++){
var x, y , r1 , r2;
var id = locationcity[i].id;
var coordinate_x = locationcity[i].value.split(",");
var coordinate_y = locationcity[j].value.split(",");
x = coordinate_x[0] - coordinate_y[0];
y = coordinate_x[1] - coordinate_y[1];
r1 = Math.pow(x, 2);
r2 = Math.pow(y, 2);
result[i] = Math.sqrt(r1 + r2);
}
}
var closest = result.sort();
$(closest).each(function(){
var _this = $(this);
console.log(_this);
});
}
findClosest(locationcity);
Appreciate any help or guidance on this. Thank you!