score:0

d3.csv will not return the data for you to assign it as you're trying to do. The reason for it is that d3.csv is an asynchronous function.

An asynchronous function does not return its result. Indeed, asynchronous function usually corresponds to potentially long operations (just like loading a possibly huge csv file in this case). If the function was to return only after the operation was over, so that it can return the result, the browser would be stuck waiting for the operation without being able to react to any other user input (imagine not being able to scroll while waiting to downlad a file!)

One common way for an asynchronous function to provide a result is through a callback: a function passed as an argument, that will be called when the result will be available, with this result as an argument. You're already using this callback: that's the function that does the logging.

The solution for you is thus to put all of your code that use the dataset inside that callback. Like that:

d3.csv("example.csv", function(d) {
  return {
    year: new Date(+d.Year, 0, 1), // convert "Year" column to Date
    make: d.Make,
    model: d.Model,
    length: +d.Length // convert "Length" column to number
  };
}, function(error, dataset) {
  // put here the code that makes use the dataset
  // e.g. drawing a graph, or showing a table

  // you won't be able to use dataset out of this function!
});

Related Query