min查找不为0的最

min查找不为0的最

本文介绍了使用D3.min查找不为0的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用D3查找数据集中的最低值.但是,我的值也为0,但我希望D3查找不为0的最低值.

I'm trying to use D3 to find the lowest value in my dataset. However, I also have values that are 0, but I want D3 to find the lowest value that is not 0.

当前我正在使用:

d3.min(data,function(d){return d.houseValues;})

d3.min(data, function(d) {return d.houseValues; })

但是很明显,有时在找到0时返回0.

But obviously this returns 0 sometimes, when a 0 is found.

有没有办法做到这一点?还是唯一的构建带有if语句以忽略0值的普通for循环的解决方案??

Is there a way to do this? Or is the only solution to build a normal for-loop with an if-statement to ignore the 0 values..?

谢谢!

推荐答案

您可以使用常量Infinity,因为Math.min(Infinity, someNumber)始终返回someNumber(除非someNumber也是无穷大).所以看起来像这样:

You can use the constant Infinity, since Math.min(Infinity, someNumber) always return someNumber (unless someNumber is also infinity). So it'll look like this:

smallest = d3.min(data, function(d) {return d.houseValues || Infinity; })

如果需要,您可以检查smallest == Infinity,如果所有房屋价值均为0,则为true.

If needed, you can check smallest == Infinity, which would be true in the case that all house values were 0.

这篇关于使用D3.min查找不为0的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:20