Created
May 9, 2019 06:27
-
-
Save bhskt/3a390a1c9025eaaea06a8b40e412df63 to your computer and use it in GitHub Desktop.
Flatten An Arbitrarily Nested Numerical Array
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
function flatten( array ) { | |
const output = []; | |
if( !( array instanceof Array ) ) | |
return false; | |
function recurseFlatten( array ) { | |
array.forEach( (el) => { | |
if( el instanceof Array ) { | |
recurseFlatten( el ); | |
} else if ( typeof el === 'number' ) { | |
output.push( el ); | |
} | |
}); | |
} | |
recurseFlatten( array ); | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment