Use D3.min to find the lowest value that is not 0

I am trying to use D3 to find the lowest value in my dataset. However, I also have values โ€‹โ€‹equal to 0, but I want D3 to find the smallest value that is not 0.

I am currently using:

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

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

Is there any way to do this? Or is it the only solution to build a normal for loop with an if statement to ignore the values โ€‹โ€‹of 0 ..?

Thanks!

+5
source share
2 answers

You can use the Infinity constant, since Math.min(Infinity, someNumber) always returns someNumber (if someNumber also infinity). So it will look like this:

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

If necessary, you can check smallest == Infinity , which will be true if all the values โ€‹โ€‹of the house are 0.

+7
source

Try filtering data first to remove zeros, e.g.

 var noZeroes = data.filter(function(d) { return d.houseValues !== 0; }); d3.min(noZeroes, function(d) {return d.houseValues; }) 
+1
source

Source: https://habr.com/ru/post/1216115/


All Articles