How to convert array with numbers as strings and strings to only numbers array in JavaScript

1 Answer

0 votes
function ToNumbersOnly(arr) {
  	return arr.map(Number).filter(Boolean);
}

const arr = ["3", "-7", "124", "0.09", "javascript", "3.14", "c++"]

console.log(ToNumbersOnly(arr));
 
  
   
/*
run:
   
[3, -7, 124, 0.09, 3.14]
   
*/

 



answered Dec 22, 2021 by avibootz
...