Created
June 27, 2016 07:52
-
-
Save regonn/dd30b1a662ee223aa31aba14ebd91e10 to your computer and use it in GitHub Desktop.
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 steamrollArray(arr) { | |
| while (hasArrElement(arr)){ | |
| arr = expandArrElement(arr); | |
| } | |
| return arr; | |
| } | |
| function hasArrElement(arr) { | |
| for (var i = 0; i < arr.length; i++ ) { | |
| if (Array.isArray(arr[i])){ | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| function expandArrElement(arr) { | |
| var returnArr = []; | |
| while (arr.length > 0) { | |
| if (Array.isArray(arr[0])){ | |
| var arrElement = arr.shift(); | |
| while (arrElement.length > 0) { | |
| returnArr.push(arrElement.shift()); | |
| } | |
| } else { | |
| returnArr.push(arr.shift()); | |
| } | |
| } | |
| return returnArr; | |
| } | |
| steamrollArray([1, [2], [3, [[4]]]]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment