From a fetched array of flight-data items I was able to created a table with each row displaying its specific flight-data alongside a button for booking the related flight.
I want to add each button into an array so that I can use the array in order to 1) access the currently booked flight flight and 2) add/display it to/at a cart.
The problem I have is that the array bookedFlights never gets appended so I can't access the button that's been clicked to use later.
How could one access the specific flight-data which relates to the button that has been clicked?
const fl = fetch("https://mocki.io/v1/fa695c4b-475e-4dfc-8608-952c0c5a28b5");
fl.then((response) => {
  return response.json();
}).then((result) => {
  const fakeFlights = result.flights;
  const table = document.createElement("table");
  const thead1 = document.createElement("thead");
  const thead2 = document.createElement("thead");
  const tbody = document.createElement("tbody");
  const bookingButtons = [];
  let row1 = document.createElement("tr");
  let row2 = document.createElement("tr");
  let headingTH = document.createElement("th");
  headingTH.innerText = "Available Flights";
  headingTH.colSpan = 7;
  row1.appendChild(headingTH);
  thead1.appendChild(row1);
  for (key in fakeFlights[0]) {
    let heading = document.createElement("th");
    if (key === "flightNumber") {
      key = "Flight Number"
    }
    if (key === "departureTime") {
      key = "Departure Time"
    }
    if (key === "arrivalTime") {
      key = "Arrival Time"
    }
    heading.innerText = key;
    row2.appendChild(heading);
  }
  let heading = document.createElement("th");
  heading.innerText = "Book the flight";
  row2.appendChild(heading);
  thead2.appendChild(row2);
  for (let i = 0; i < fakeFlights.length; i++) {
    let row = document.createElement("tr");
    for (key in fakeFlights[i]) {
      let rowData = document.createElement("td");
      rowData.innerHTML = fakeFlights[i][key];
      row.appendChild(rowData);
    }
    let btn = document.createElement("button");
    btn.innerText = "Book";
    btn.id = i;
    bookingButtons.push(btn);
    row.appendChild(btn);
    tbody.appendChild(row);
  }
  table.append(thead1, thead2, tbody);
  document.body.appendChild(table);
  let bookedFlights = [];
  for (let selectedButton of bookingButtons) {
    selectedButton.addEventListener("click", function() {
      bookedFlights.push(selectedButton.id);
    });
  }
  const data = {
    flights: fakeFlights,
    bookedFlights: bookedFlights
  }
  return data;
}).then((data) => {
  console.log(data.flights[data.bookedFlights[0]]);
}).catch(() => {
  document.write("error")
});
 
    