使用d3.js在此n2
对象中查找dataJSON3
(450)的最大值的最佳方法是什么?
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},
]
}
答案 0 :(得分:2)
如果你想找到这个json结构的最大值,你可以这样做:
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`
]);
对于动态数量的集合,您需要遍历每个属性,找到其最大值,然后在每个属性的最大值中找到最大值:
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);