Created
December 17, 2015 19:41
-
-
Save Narshe1412/75734adfdbd4c75bd6cc to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/narshe1412 's solution for Bonfire:Steamroller
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 steamroller(arr) { | |
// I'm a steamroller, baby | |
return arr.reduce(function (flat, toFlatten) { | |
return flat.concat(Array.isArray(toFlatten) ? steamroller(toFlatten) : toFlatten); //ternary | |
}, []); | |
/* | |
Uses the same concat flatten example on the MDN, but with recursion to further flatten | |
ex: | |
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) { | |
return a.concat(b); | |
}, []); | |
In this case b is checked if is still an array, then steamroller is called again if it is. | |
Otherwise it's just returned | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References:
http://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat