Last active
August 29, 2015 14:15
-
-
Save hammeiam/e7df5e2b8872a461e3ce to your computer and use it in GitHub Desktop.
A simple implementation of flatten in javascript
This file contains hidden or 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(arr){ | |
var output = []; | |
for (var i = 0; i < arr.length; i++) { | |
if(arr[i] instanceof Array){ | |
output = output.concat(flatten(arr[i])) | |
} else { | |
output.push(arr[i]); | |
} | |
}; | |
return output; | |
} | |
// console.log( flatten([1,[2,3,[4,5]],6]) ) -> [1,2,3,4,5,6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment