Last active
March 13, 2017 03:09
-
-
Save marcoslhc/a01edd52d51d9fb501b3d940d9aca1a8 to your computer and use it in GitHub Desktop.
flattenArray.js
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 flattenArray(array) { | |
| let temp = []; | |
| function flatten(arr) { | |
| if (Array.isArray(arr)) { | |
| const [head, tail] = [arr[0], arr.slice(1)] | |
| flatten(head); | |
| if (tail.length > 0) flatten(tail); | |
| } else { | |
| temp.push(arr); | |
| } | |
| } | |
| flatten(array); | |
| return temp; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment