In JavaScript, I have an array containing boolean values and integer numbers, eg
[1, true, 2, false, 3]
I want to get an array with the boolean values removed. I tried
> [1, true, 2, false, 3].filter(Number)
[ 1, true, 2, 3 ]
Strangely, JavaScript thinks that false
is not a Number, but true is. How so?
If my array contains strings, it works.
> [1, '', 2, 'foo', 3].filter(Number)
[ 1, 2, 3 ]
I know I can write the following, I was hoping for an easier way.
> [1, true, 2, false, 3].filter(x => typeof x === 'number')
[ 1, 2, 3 ]
What's the easiest way to achieve what I want?
Thanks