Reputation: 163
What's the best way to find the maximum value of n2
(450) in this dataJSON3
object using d3.js?
var dataJSON3 = {
set1 : [
{"nom":"Allemagne", "n1":200, "n2":86, "n3": 30},
{"nom":"Allemagne", "n1":120, "n2":156, "n3": 40},
{"nom":"France", "n1":117, "n2":34, "n3": 35},
{"nom":"Italie", "n1": 309, "n2":12, "n3": 6},
],
set2 : [
{"nom":"Suisse", "n1":60, "n2":86, "n3": 30},
{"nom":"Allemagne", "n1":90, "n2":450, "n3": 40},
{"nom":"France", "n1":1000, "n2":34, "n3": 35},
{"nom":"Italie", "n1": 440, "n2":12, "n3": 6},
]
}
Upvotes: 2
Views: 3202
Reputation: 5822
If you want to find the max of exactly this json structure, you can do it like this:
var maxValue = d3.max([ // Find the max value of a list of 2 elements
d3.max(dataJSON3.set1, function(d) { return d.n2; }), // Find the max value in `set1`
d3.max(dataJSON3.set2, function(d) { return d.n2; }) // Find the max value in `set2`
]);
For a dynamic number of sets, you'll need to iterate through each property, find its max value and then find the max values among each property's max value:
var maxValuesPerSet = [];
for (var property in dataJSON3) {
if (object.hasOwnProperty(property)) {
maxValuesPerSet.push(d3.max(dataJSON3[property], function(d) { return d.n2; }));
}
}
var maxValue = d3.max(maxValuesPerSet);
Upvotes: 3