Search code examples
arraysd3.jsmaxcol

D3 Max giving error while accessing column wise and header array


I have a csv file data. I want to find maximum of each column, when I am directly writing the header with the help of d3.max to find maximum it's working fine, but when I am storing header into an array and then finding maximun through the header stored in array, then its not working.

Suppose my data is :

data = foo,bar
       12,14
       26,75
       67,11
       19,42

I stored the header value in an array :

headerarr = [ "foo", "bar"]

When I am calculation like below, it is giving me correct value

var maxarr = d3.max(data, function(d) { return d.foo; } ); //Working Fine

But when I am trying to find the value by getting header data from array, its not working.

var maxarr = d3.max(data, function(d) { return d.headerarr[0]; } );//Not Working

What is the correct way to access the array data so that it also give me the same result.


Solution

  • try

    var maxarr = d3.max(data, function(d) { return d[headerarr[0]]; } );