D3 js从包含特定键值的对象获取d3.max [英] D3 js get a d3.max from an object that contains a specific key value
本文介绍了D3 js从包含特定键值的对象获取d3.max的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据集,并尝试获取d3.max来指定比例域,但仅从具有key enabled = true的对象中获取d3.max值.如何实现?
I have the following dataset and am trying to get the d3.max to specify a domain for a scale, but want the d3.max value only from objects that have the key enabled=true. How can this be achieved?
[{
"key": "name1",
"enabled": "true",
"values": [{
"date": "2016-09-13T02:11:44Z",
"value": "100",
}, {
"date": "2016-09-13T02:11:44Z",
"value": "200",
}]
}, {
"key": "name2",
"enabled": "false",
"values": [{
"date": "2016-09-13T02:11:44Z",
"value": "400",
}, {
"date": "2016-09-13T02:11:44Z",
"value": "500",
}]
}]
我认为我应该以某种方式使用钥匙,但是还没有做到.有人可以指出我正确的方向吗?这就是我到目前为止所拥有的...
I think I am supposed to use a key somehow, but haven't been able to do it. Can someone point me to the right direction? This is what I have so far...
d3.max(dataset, function(c) { return d3.max(c.values, function(d) { return d.value; }); })
推荐答案
如果您需要一行:
var max = d3.max(data.filter(d => d.enabled === "true"), e => d3.max(e.values, f => f.value));
或者,没有箭头功能:
var max = d3.max(data.filter(function(d) {
return d.enabled === "true";
}), function(e) {
return d3.max(e.values, function(f) {
return f.value;
});
});
这是一个演示:
var data = [{
"key": "name1",
"enabled": "true",
"values": [{
"date": "2016-09-13T02:11:44Z",
"value": "100",
}, {
"date": "2016-09-13T02:11:44Z",
"value": "200",
}]
}, {
"key": "name2",
"enabled": "false",
"values": [{
"date": "2016-09-13T02:11:44Z",
"value": "400",
}, {
"date": "2016-09-13T02:11:44Z",
"value": "500",
}]
}];
var max = d3.max(data.filter(d => d.enabled === "true"), e => d3.max(e.values, f => f.value));
console.log(max);
<script src="https://d3js.org/d3.v4.min.js"></script>
它使用enabled: "true"
接受多个对象,请检查第二个演示,我在您的数据数组中放置了另一个对象:
It accepts more than one object with enabled: "true"
, check this second demo, I put another object in your data array:
var data = [{
"key": "name1",
"enabled": "true",
"values": [{
"date": "2016-09-13T02:11:44Z",
"value": "100",
}, {
"date": "2016-09-13T02:11:44Z",
"value": "200",
}]
}, {
"key": "name2",
"enabled": "false",
"values": [{
"date": "2016-09-13T02:11:44Z",
"value": "400",
}, {
"date": "2016-09-13T02:11:44Z",
"value": "500",
}]
},{
"key": "name1",
"enabled": "true",
"values": [{
"date": "2016-09-13T02:11:44Z",
"value": "800",
}, {
"date": "2016-09-13T02:11:44Z",
"value": "200",
}]
}];
var max = d3.max(data.filter(d => d.enabled === "true"), e => d3.max(e.values, f => f.value));
console.log(max);
<script src="https://d3js.org/d3.v4.min.js"></script>
这篇关于D3 js从包含特定键值的对象获取d3.max的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文