How to extract only numbers from an array in Node.js

1 Answer

0 votes
const arr = [2, 'nodejs', 34, null, 9387, 20];

const onlyNumbers = arr.filter(element => typeof element === 'number');

console.log(onlyNumbers); 


  
  
  
/*
run:

[ 2, 34, 9387, 20 ]
  
*/

 



answered May 9, 2022 by avibootz
...