Thank you for your feedback DanCouper. Below is the first 3 lines of the source data.
ID,AirlineID,AirlineName,AirlineCountry,SourceAirportID,SourceAirportCode,SourceAirport,SourceCity,SourceCountry,SourceLatitude,SourceLongitude,DestAirportID,DestCode,DestAirport,DestCity,DestCountry,DestLatitude,DestLongitude
1,24,American Airlines,United States,4355,ABE,Lehigh Valley International Airport,Allentown,United States,40.65209961,-75.44080353,3876,CLT,Charlotte Douglas International Airport,Charlotte,United States,35.2140007,-80.94309998
2,24,American Airlines,United States,4355,ABE,Lehigh Valley International Airport,Allentown,United States,40.65209961,-75.44080353,3752,PHL,Philadelphia International Airport,Philadelphia,United States,39.87189865,-75.2410965
3,24,American Airlines,United States,3718,ABI,Abilene Regional Airport,Abilene,United States,32.41130066,-99.68190002,3670,DFW,Dallas Fort Worth International Airport,Dallas-Fort Worth,United States,32.896801,-97.03800201
Below is all of the code in it’s entirety. It’s part of a self study assignement on using D3 to analyze a csv file with flight data. I was able to work the showData function could probably do the rest of it. However, I wanted to fully understand how and why all aspects of the code works. Thank you.
<script>
let store = {};
function loadData() {
//TODO 1: Add the code to load the CSV file named "routes.csv" | 1 Line
let promise = d3.csv("routes.csv");
return promise.then(routes => {
//TODO 2: Save the routes into our store variable;
store.routes = routes;
return store;
})
}
function groupByAirline(data) {
//Iterate over each route, producing a dictionary where the keys are the ailines ids and the values are the information of the airline.
let result = data.reduce((result, d) => {
let currentData = result[d.AirlineID] || {
"AirlineID": d.AirlineID,
"AirlineName": d.AirlineName,
"Count": 0
}
//TODO: Increment the count (number of routes) of ariline.
// currentData.Count +=
//TODO: Save the updated information in the dictionary using the airline id as key.
// result[d.AirlineID] =
return result;
}, {})
//We use this to convert the dictionary produced by the code above, into a list, that will make it easier to create the visualization.
// result = Object.keys(result).map(key => result[key])
// result = //TODO: Sort the data in descending order of count.
return result;
}
function showData() {
//Get the routes from our store variable
let routes = store.routes
console.log(routes);
// Compute the number of routes per airline.
let airlines = groupByAirline(store.routes);
console.log(airlines)
}
loadData().then(showData);
</script>