Rachelle Uy
Rachelle Uy

Reputation: 858

D3 - Returning the whole array with the max count

I'm new to D3 JS and having trouble looking for the solution to this.

I have the following pie data object:

pieData = [
    { title: "U", count: 10 },
    { title: "D", count: 30 },
    { title: "N", count: 50 }
];

I simply need to return the array with the highest count:

{ title: "N", count: 50 }

Now if I execute the following code, it only returns the maximum count. But I need to access the whole array so I can print the title and the count.

var maxData = d3.max(pieData, function(d) {
    d.count;
});

I'm sure the solution to this is really simple. I hope some of you can share a possible solution or some alternatives.

Upvotes: 0

Views: 209

Answers (1)

Ray Waldin
Ray Waldin

Reputation: 3227

This isn't really d3 specific, but you could just do:

var highest = pieData.reduce(function(highest, item) {
    return item.count > highest.count ? item : highest;
}, pieData[0]);

Upvotes: 3

Related Questions