Layne
Layne

Reputation: 672

Getting max and min from two different sets in json

I haven't found a solution with data set up quite like mine...

var marketshare = [
    {"store": "store1", "share": "5.3%", "q1count": 2, "q2count": 4, "q3count": 0},
    {"store": "store2","share": "1.9%", "q1count": 5, "q2count": 10, "q3count": 0},
    {"store": "store3", "share": "2.5%", "q1count": 3, "q2count": 6, "q3count": 0}
];

Code so far, returning undefined...

var minDataPoint = d3.min( d3.values(marketshare.q1count) ); //Expecting 2 from store 1 
var maxDataPoint = d3.max( d3.values(marketshare.q2count) ); //Expecting 10 from store 2

I'm a little overwhelmed by d3.keys, d3.values, d3.maps, converting to array, etc. Any explanations or nudges would be appreciated.

Upvotes: 0

Views: 45

Answers (1)

mdml
mdml

Reputation: 22892

I think you're looking for something like this instead:

d3.min(marketshare, function(d){ return d.q1count; }) // => 2.

You can pass an accessor function as the second argument to d3.min/d3.max.

Upvotes: 3

Related Questions