Skip to content

Instantly share code, notes, and snippets.

@LautaroJayat
Created January 27, 2020 18:49
Show Gist options
  • Save LautaroJayat/700d00b8c7ccf8ba0bc562ed879caafb to your computer and use it in GitHub Desktop.
Save LautaroJayat/700d00b8c7ccf8ba0bc562ed879caafb to your computer and use it in GitHub Desktop.
Array Question
let array = [1, 2, 3, '4', undefined, 'a', [], null];
function onlyNumbers(arr) {
let output = [];
// If 'isNaN()' doesn't return true for the given element, we pushit to the output array
arr.forEach(e => { if (!isNaN((e))) { output.push((e)) } })
return output
}
console.log(onlyNumbers(array));
// Wich outputs [ 1, 2, 3, '4', [], null];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment