I have an array of 3 numbers. I would like to get the smallest and the second smallest numbers from this array provided the number is not 0
.
Let's say my array is as follows:
[459, 25, 0]
In this case I would like 25
reported as smallest and 459
reported as second smallest. I am able to get 25 as the smallest like this:
var arr = [459, 25, 0];
var smallest = Math.min.apply(null, arr.filter(Boolean));
console.log(smallest);
But how would I go about getting the second smallest number that isn't 0
? Here is what I tried, but this returns 0
.
var arr = [459, 25, 0];
var smallest = Math.min.apply(null, arr.filter(Boolean));
var secSmallest = Math.min.apply(null, arr.filter(n => n != smallest));
console.log(secSmallest);