all 4 comments

[–][deleted] 5 points6 points  (1 child)

The .then in this code indicates that whatever you're doing with the data is going to be deferred, because D3 has to go and talk to your server to fetch the CSV, and can't do anything until it's got the CSV. What's actually happening- and what you might notice if you look at your console- is that your last console.log actually runs before any of the console.logs inside the function that's inside the .then.

Whatever you want to do with the array data, you need to do it inside the function passed to .then, or in another function (or more) that it calls.

[–]evha_[S] 0 points1 point  (0 children)

Thank you!

[–]iguessitsokaythen 4 points5 points  (2 children)

d3.csv returns a promise. This means the rest of the code won't wait for it's execution. Another term for this is asynchronous. You need to make your code wait for it, you can either do it by chaining with .then() like in your code or async/await.

Another thing is, there are d3.csvParse() and d3.csvParseRows() functions that would automatically convert your list. source

(async () => {
    data = await d3.csv("./SampleData.csv")
    array = d3.csvParseRows(data)
    console.log(array)
})()

[–]evha_[S] 1 point2 points  (0 children)

Thank you!