简体   繁体   中英

How do I retrieve an array from a csv file in javascript using d3?

Having spent hours on Google and SO, I have finally cracked.

I have a PSQL table of outbound flight data of one day sorted by time and destination continent. I am going through all rows of my table, pushing the destination continent of each flight into arrays, with each array representing 15mins of flight data. for example, 1st array - 00:00 to 00:15 would be: [] ......(no flights) 21st array - 05:00 to 05:15 would be: ["NA", "NA", "NA", "EU", "AS", "EU"] etc.

all the way to 96 arrays (96 x 15 mins in 24 hours.)

I have this data pushed into a CSV file with the header being just flights however, when I try to recall the data in d3 using:

d3.csv("/my_file.csv", function(data) {
console.log(data);
})

the object returned is 96 rows of "["NA"" or something equivalent; very obviously it is breaking at the very first comma. Is there a way to retrieve arrays from a CSV file using d3??

CSV files work best when there is a fixed number of columns in each row. For your data, can you use a JSON file instead? There are different ways you could represent your data as JSON, for example, you could use an object with each property being an array:

{
    "00:00 to 00:15": [],
    "05:00 to 05:15": ["NA", "NA", "NA", "EU", "AS", "EU"]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM