gpt4 book ai didi

javascript - 按特定属性的最大值过滤数组元素

转载 作者:行者123 更新时间:2023-11-29 23:08:47 24 4
gpt4 key购买 nike

我想将每个元素子集化为一个数组,这样我就可以得到与一个属性的最大值相关联的所有值。我将在 D3.js 图形中使用它,该图形将在我的数据中每个分组的最大值处进行注释。

例如,这是我如何按物种嵌套 Iris 数据集,然后仅取 SepalLength 的最大值。

maxdata = d3.nest()
.key(function (d) { return d.species; })
.rollup(function (leaves) {
var maxSepalLength = d3.max(leaves, function (x) { return x.sepalLength })
return { maxSepalLength:maxSepalLength}
})
.entries(irisdata)

输出看起来像这样:

0:
key: "setosa"
value:
maxSepalLength: 5.4
1:
key: "versicolor"
value:
maxSepalLength: 6.8
2:
key: "virginica"
value:
maxSepalLength: 7.9

这基本上按预期工作,但我不确定如何保留其他变量。我想要的输出是这样的:

0:
key: "setosa"
value:
maxSepalLength: 5.4
sepalWidth: <value>
petalLength: <value>
petalWidth: <value>

...etc

数据的 fiddle 是here .我怎样才能坚持这些其他值(value)观?

最佳答案

您可以在 leaves 数组中查找具有 maxSepalLength 的对象,并将其添加到返回的对象中:

return { maxSepalLength , ...leaves.find(e => e.sepalLength === maxSepalLength) }

编辑:

您可以使用 reduce 而不是 d3.max 来减少迭代次数:

maxdata = d3.nest()
.key(function (d) { return d.species; })
.rollup(function (leaves) {
return leaves.reduce((max, curr) => {
max['maxSepalLength'] = max['maxSepalLength'] || 0;

if(max['maxSepalLength'] < curr.sepalLength)
max = {...curr, maxSepalLength : curr.sepalLength};

return max;
}, {});
})
.entries(irisdata)

var irisdata = d3.csvParse(d3.select("pre#data").text());
console.log({ irisdata })

irisdata.forEach(function (d) {
d.sepalLength = +d.sepalLength;
});

maxdata = d3.nest()
.key(function (d) { return d.species; })
.rollup(function (leaves) {

var maxSepalLength = d3.max(leaves, function (x) { return x.sepalLength })
return { maxSepalLength , ...leaves.find(e => e.sepalLength === maxSepalLength) }
})
.entries(irisdata)

console.log({maxdata})
pre {
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<pre id="data">
sepalLength,sepalWidth,petalLength,petalWidth,species
5.1,3.5,1.4,0.2,setosa
4.9,3,1.4,0.2,setosa
4.7,3.2,1.3,0.2,setosa
4.6,3.1,1.5,0.2,setosa
5,3.6,1.4,0.2,setosa
5.4,3.9,1.7,0.4,setosa
4.6,3.4,1.4,0.3,setosa
5,3.4,1.5,0.2,setosa
5.3,3.7,1.5,0.2,setosa
5,3.3,1.4,0.2,setosa
6.8,2.8,4.8,1.4,versicolor
6.7,3,5,1.7,versicolor
6,2.9,4.5,1.5,versicolor
5.7,2.6,3.5,1,versicolor
5.5,2.4,3.8,1.1,versicolor
5.5,2.4,3.7,1,versicolor
5.8,2.7,3.9,1.2,versicolor
6,2.7,5.1,1.6,versicolor
5.4,3,4.5,1.5,versicolor
6,3.4,4.5,1.6,versicolor
6.7,3.1,4.7,1.5,versicolor
6.3,2.3,4.4,1.3,versicolor
5.6,3,4.1,1.3,versicolor
5.5,2.5,4,1.3,versicolor
5.5,2.6,4.4,1.2,versicolor
6.1,3,4.6,1.4,versicolor
5.8,2.6,4,1.2,versicolor
5,2.3,3.3,1,versicolor
5.6,2.7,4.2,1.3,versicolor
5.7,3,4.2,1.2,versicolor
5.7,2.9,4.2,1.3,versicolor
6.2,2.9,4.3,1.3,versicolor
5.1,2.5,3,1.1,versicolor
5.7,2.8,4.1,1.3,versicolor
6.3,3.3,6,2.5,virginica
5.8,2.7,5.1,1.9,virginica
7.1,3,5.9,2.1,virginica
5.8,2.8,5.1,2.4,virginica
6.4,3.2,5.3,2.3,virginica
6.5,3,5.5,1.8,virginica
7.7,3.8,6.7,2.2,virginica
7.7,2.6,6.9,2.3,virginica
6,2.2,5,1.5,virginica
6.9,3.2,5.7,2.3,virginica
5.6,2.8,4.9,2,virginica
7.7,2.8,6.7,2,virginica
6.3,2.7,4.9,1.8,virginica
6.7,3.3,5.7,2.1,virginica
7.2,3.2,6,1.8,virginica
6.2,2.8,4.8,1.8,virginica
6.1,3,4.9,1.8,virginica
6.4,2.8,5.6,2.1,virginica
7.2,3,5.8,1.6,virginica
7.4,2.8,6.1,1.9,virginica
7.9,3.8,6.4,2,virginica
6.4,2.8,5.6,2.2,virginica
6.3,2.8,5.1,1.5,virginica
6.1,2.6,5.6,1.4,virginica
7.7,3,6.1,2.3,virginica
6.3,3.4,5.6,2.4,virginica
6.4,3.1,5.5,1.8,virginica
</pre>

关于javascript - 按特定属性的最大值过滤数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54224676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com