I have two airport datasets – one stored locally and another one fetched from a server.
The local dataset, airports
, has around 45,000 individual objects, where each object has the following structure:
{
"ident": "AYPY",
"type": "large_airport",
"name": "Port Moresby Jacksons International Airport",
"latitude_deg": -9.443380356,
"longitude_deg": 147.2200012,
"iso_country": "PG"
},
The fetched dataset is created using the createDestinationObject(title)
, which fetches the related data from the title
page and stores it in the finalList
variable. This is how it looks:
{
"ICAO": "AYPY",
"name": "Port Moresby Jacksons International Airport",
"latitude": 0,
"longitude": 0,
},
The fetched dataset lacks coordinates (hence the placeholder zeroes). I want to take the coordinates from the airports
dataset by matching airports.ident
with finalList.ICAO
, as they both are standard ICAO codes for airports. Then I assign the latitude_deg
and longitude_deg
values from the matching airports
item to the latitude
and longitude
properties of the finalList
item.
To try and solve this, I used a nested for loop where each finalList
item is looped through the airports
dataset, and if the ICAO code matches, it assigns the coordinates. The code works, but The problem is that it takes quite some time (even up to 20-30 seconds if finalList
dataset has >300 objects. Is there any way to solve this faster, without looping every finalList
item through each of the +40k items in the airports
dataset?
async function assignCoordinates(title) {
var finalList = await createDestinationObject(title);
for (var destination in finalList) {
for (var airport in airports) {
if (finalList[destination].ICAO === airports[airport].ident) {
finalList[destination].latitude = airports[airport].latitude_deg;
finalList[destination].longitude = airports[airport].longitude_deg;
}
}
}
console.log(finalList);
}
I am extremely new to Javascript, so my code might be sloppy in general, don’t hesitate to critique.