Alex
Alex

Reputation: 303

d3.js csv loading - simple data conversion

I am loading a csv file by using d3.csvfunction

d3.csv(files[0],
    function (d) {

        return {
            col1: +d["col1"],
            col2: +d["col2"],
            col3: +d["col3"]            
        };           
    },
function(error, data) {...});

where I use function(d) to convert all the data to numerics. Is there a way to simplify the code and process all the columns without explicitly referring to their names?

Upvotes: 2

Views: 113

Answers (1)

Bengt
Bengt

Reputation: 4038

You want to do the same thing repeatedly. This calls for a loop (e.g. a for-loop or a map).

function (d) {
  var result = {};
  for (key in d) {
    result[key] = +d[key];
  }
  return result;
}

Upvotes: 3

Related Questions