Nakatomi
Nakatomi

Reputation: 104

Possible to put defined variable in array call

I have a quick question. here is my code:

d3.csv("test1.csv", function(data) {



            var year = "state";


        .domain([
                d3.min(data, function(d) { return d.value; }), 
                d3.max(data, function(d) { return d.value; })
            ]);

I am querying a csv file here. d3.min and max are returning the coloumn named "value" using "return d.value;".

Is it possible to have my year variable defined above this code in this call instead of hard coding the word "value" from the column name?

I would like to use the variable instead as then I could change it with one change.

I have tried return d.year but this obviously looks for a column called year and not my variable called year.

thank you.

Upvotes: 0

Views: 33

Answers (1)

Sirko
Sirko

Reputation: 74046

Use the bracket syntax:

var year = "state";
// ...
.domain([
  d3.min(data, function(d) { return d[ year ]; }), 
  d3.max(data, function(d) { return d[ year ]; })
]);

d.year refers to the property year, as it is just an identifier.

If you want to refer the variable year and thus use its content as the identifier, you have to use the bracket syntax.

Upvotes: 3

Related Questions