Skip to content

Instantly share code, notes, and snippets.

@LautaroJayat
Created January 27, 2020 19:25
Show Gist options
  • Save LautaroJayat/3c3d8dfea27e580942cf155ec63093e6 to your computer and use it in GitHub Desktop.
Save LautaroJayat/3c3d8dfea27e580942cf155ec63093e6 to your computer and use it in GitHub Desktop.
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