Created
January 27, 2020 19:25
-
-
Save LautaroJayat/3c3d8dfea27e580942cf155ec63093e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [1, 2, 3, '4', undefined, 'a', [], null]; | |
function onlyNumbers(arr) { | |
let output = []; | |
// Now we try to parseInt() each element, | |
// and push a new Number() created with e as an argument. | |
arr.forEach(e => { if (parseInt(e)) { output.push(Number(e)) } }) | |
return output | |
} | |
console.log(onlyNumbers(array)); | |
// Wich outputs [ 1, 2, 3, 4 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment