Skip to content

Instantly share code, notes, and snippets.

@jeradg
Created March 25, 2013 23:23
Show Gist options
  • Save jeradg/5241811 to your computer and use it in GitHub Desktop.
Save jeradg/5241811 to your computer and use it in GitHub Desktop.
JavaScript function for flattening an array.
var flatten = function( array ) {
var flattenedArray = [ ];
var flattenLoop = function( i ) { i.forEach( function( j ) {
if ( Object.prototype.toString.call( j ) === '[object Array]' ) {
flattenLoop( j );
} else {
flattenedArray.push( j );
}
});
};
flattenLoop( array );
return flattenedArray;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment