Skip to content

Instantly share code, notes, and snippets.

@bhskt
Created May 9, 2019 06:27
Show Gist options
  • Save bhskt/3a390a1c9025eaaea06a8b40e412df63 to your computer and use it in GitHub Desktop.
Save bhskt/3a390a1c9025eaaea06a8b40e412df63 to your computer and use it in GitHub Desktop.
Flatten An Arbitrarily Nested Numerical Array
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