Sitemap

How to filter numbers out of an array OR return only the numbers from an array using JavaScript

1 min readNov 10, 2018

--

If you’re in a hurry, here’s the code you need…

To filter out numbers from an array:

const arr = [1, 2, 3, 'a', 4, 'b', 5, undefined, null];arr.filter(x => !(parseInt(x) == x)).filter(x => x);
// => ['a', 'b']

To return only the numbers in an array:

const arr = [1, 2, 3, 'a', 4, 'b', 5, undefined, null];arr.filter(x => (parseInt(x) == x));
// => [1, 2, 3, 4, 5]

--

--

No responses yet