I have an array of arbitrary elements and need to get only number elements.
I tried arr.filter((c) => !isNaN(parseInt(arr[c]))); but that didn't work. I still have a full array. What is wrong here and what else can I do?
The first argument in the callback to .filter is the array item being iterated over - if c is the array item, then referencing arr[c] usually doesn't make much sense. Try using a simple typeof check instead:
const arr = [3, 'foo', { bar: 'baz' }, false, 4, 5];
console.log(arr.filter(item => typeof item === 'number'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With