dmartinez
dmartinez

Reputation: 287

d3 read .csv header programmatically

How can I read a csv header and set it's attributes programatically from my d3.csv call?

Here is how I call now:

d3.csv("data/att2.csv", function(d) {
    return {
        Id: d.Id,
        ProposedMass: d["Proposed"],
        MostProbablyCompound: d["Most"],
        Probability: +d["Prob"],
        Entropy: d.E,
        Formula: d.For,
        IonAnnotation: d["I"]
    };
}, 
function(error, rows) {
    window["data"] = rows;    
});

What I want is to key all csv header (keys) and set it's values automatically (instead of making it manually, as one may see in my code Id:d.Id, etc. Then, if my csv changes, I don't need to recode it.

Upvotes: 0

Views: 1255

Answers (1)

dmartinez
dmartinez

Reputation: 287

This is the answer I was looking for:

d3.csv("data/att2.csv", function(data) {

    return data;        

}, function(error, rows) {
  window["data"] = rows;

});

Then you can separate all keys using d3.keys as a function :) Hope it helps someone else.

Upvotes: 2

Related Questions