How to compare object properties between different objects in different arrays

I am getting data from 2 APIs, one returns the data from 18 different airports, and one returns the flighst from one airport to another (this API needs to be fed with the id of the departure airport and the arrival airport)

The API that returns the flights is like this one here:

{
    "data": [
        {
            "id": 64,
            "airlineId": 8,
            "departureAirportId": 5,
            "arrivalAirportId": 2,
            "price": 205.75
        },
        {
            "id": 42,
            "airlineId": 7,
            "departureAirportId": 2,
            "arrivalAirportId": 17,
            "price": 1324.42
        }
    ]
}

While The API that returns the airports is this one:

{
    "data": [
        {
            "id": 1,
            "codeIata": "PSA",
            "latitude": 43.6931,
            "longitude": 10.3789
        },
        {
            "id": 2,
            "codeIata": "BLQ",
            "latitude": 44.5357,
            "longitude": 11.2736
        },
        {
            "id": 3,
            "codeIata": "BGY",
            "latitude": 45.6702,
            "longitude": 9.6715
        },
        {
            "id": 4,
            "codeIata": "MXP",
            "latitude": 45.6289,
            "longitude": 8.7183
        },
        {
            "id": 5,
            "codeIata": "TRN",
            "latitude": 45.1934,
            "longitude": 7.6158
        },
        {
            "id": 6,
            "codeIata": "VCE",
            "latitude": 45.5046,
            "longitude": 12.3444
        },
        {
            "id": 7,
            "codeIata": "FCO",
            "latitude": 41.8025,
            "longitude": 12.213
        },
        {
            "id": 8,
            "codeIata": "PSR",
            "latitude": 42.4261,
            "longitude": 14.1793
        },
        {
            "id": 9,
            "codeIata": "NAP",
            "latitude": 40.8819,
            "longitude": 14.2731
        },
        {
            "id": 10,
            "codeIata": "BRI",
            "latitude": 41.1375,
            "longitude": 16.763
        },
        {
            "id": 11,
            "codeIata": "BDS",
            "latitude": 40.6551,
            "longitude": 17.936
        },
        {
            "id": 12,
            "codeIata": "SUF",
            "latitude": 38.9057,
            "longitude": 16.2402
        },
        {
            "id": 13,
            "codeIata": "CTA",
            "latitude": 37.4667,
            "longitude": 15.0639
        },
        {
            "id": 14,
            "codeIata": "PMO",
            "latitude": 38.1813,
            "longitude": 13.0994
        },
        {
            "id": 15,
            "codeIata": "NRT",
            "latitude": 35.7653,
            "longitude": 140.386
        },
        {
            "id": 16,
            "codeIata": "KLX",
            "latitude": 37.0683,
            "longitude": 22.0256
        },
        {
            "id": 17,
            "codeIata": "ALA",
            "latitude": 43.3553,
            "longitude": 77.0447
        },
        {
            "id": 18,
            "codeIata": "PEK",
            "latitude": 40.0725,
            "longitude": 116.5975
        }
    ]
}

I need to take the “departureAirportId” and the “arrivalAirportId” from the flights API, check them against the airport API and fetch the name of the airport according the provided id. I tried in many ways but each time I ended up digging a huge rabbit hole and decided just to restart all over for I do not know how many times now.

Any suggestion how I could do this?

Answer

This should solve your problem:

let flights = {
  "data": [{
      "id": 64,
      "airlineId": 8,
      "departureAirportId": 5,
      "arrivalAirportId": 2,
      "price": 205.75
    },
    {
      "id": 42,
      "airlineId": 7,
      "departureAirportId": 2,
      "arrivalAirportId": 17,
      "price": 1324.42
    }
  ]
};


let airports = {
  "data": [{
      "id": 1,
      "codeIata": "PSA",
      "latitude": 43.6931,
      "longitude": 10.3789
    },
    {
      "id": 2,
      "codeIata": "BLQ",
      "latitude": 44.5357,
      "longitude": 11.2736
    },
    {
      "id": 3,
      "codeIata": "BGY",
      "latitude": 45.6702,
      "longitude": 9.6715
    },
    {
      "id": 4,
      "codeIata": "MXP",
      "latitude": 45.6289,
      "longitude": 8.7183
    },
    {
      "id": 5,
      "codeIata": "TRN",
      "latitude": 45.1934,
      "longitude": 7.6158
    },
    {
      "id": 6,
      "codeIata": "VCE",
      "latitude": 45.5046,
      "longitude": 12.3444
    },
    {
      "id": 7,
      "codeIata": "FCO",
      "latitude": 41.8025,
      "longitude": 12.213
    },
    {
      "id": 8,
      "codeIata": "PSR",
      "latitude": 42.4261,
      "longitude": 14.1793
    },
    {
      "id": 9,
      "codeIata": "NAP",
      "latitude": 40.8819,
      "longitude": 14.2731
    },
    {
      "id": 10,
      "codeIata": "BRI",
      "latitude": 41.1375,
      "longitude": 16.763
    },
    {
      "id": 11,
      "codeIata": "BDS",
      "latitude": 40.6551,
      "longitude": 17.936
    },
    {
      "id": 12,
      "codeIata": "SUF",
      "latitude": 38.9057,
      "longitude": 16.2402
    },
    {
      "id": 13,
      "codeIata": "CTA",
      "latitude": 37.4667,
      "longitude": 15.0639
    },
    {
      "id": 14,
      "codeIata": "PMO",
      "latitude": 38.1813,
      "longitude": 13.0994
    },
    {
      "id": 15,
      "codeIata": "NRT",
      "latitude": 35.7653,
      "longitude": 140.386
    },
    {
      "id": 16,
      "codeIata": "KLX",
      "latitude": 37.0683,
      "longitude": 22.0256
    },
    {
      "id": 17,
      "codeIata": "ALA",
      "latitude": 43.3553,
      "longitude": 77.0447
    },
    {
      "id": 18,
      "codeIata": "PEK",
      "latitude": 40.0725,
      "longitude": 116.5975
    }
  ]
};

const findAirportByID = (airportID) => {
  return airports.data.filter(item => item.id == airportID);
}

flights.data.forEach(flight => {
    let departureAirport = findAirportByID(flight.departureAirportId);
  if(departureAirport[0])
    flight.deparureAirportCode = departureAirport[0].codeIata;
  let arrivalAirport = findAirportByID(flight.arrivalAirportId);
  if(arrivalAirport[0])
    flight.arrivalAirportCode = arrivalAirport[0].codeIata;
});

console.log(flights);