Created
March 25, 2013 23:23
-
-
Save jeradg/5241811 to your computer and use it in GitHub Desktop.
JavaScript function for flattening an 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
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