Donny P Donny P - 4 months ago 30
Javascript Question

Get the minimum value of an object in d3.min

How can I get the maximum value for the key "sample"?

I can use

d3.max(myData)
to get the maximum of an array like
[1,2,3]
. For my more complex example, I can use an accessor function to get the values out of
myData
. What does this accessor function look like?

var myData = [
{
type: "a",
sample: 28,
population: 32
},
{
type: "b",
sample: 24,
population: 19
},
{
type: "c",
sample: 19,
population: 23
},
{
type: "d",
sample: 29,
population: 26
}
];

Answer

The accessor function will be evaluated for each each array element, with the element being passed as the first argument. With that in mind, you can do the following:

d3.max(myData, function(d) {
  return d.sample;
});
Comments