Last active
October 24, 2019 16:23
-
-
Save witalobenicio/6391e55a1dbfc0693486dd80bd0c4fe1 to your computer and use it in GitHub Desktop.
Deeply Flatt Array
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
// @flow | |
const flattenDeep = (arr: Array<number>], flatArr = []) => { | |
const length = !arr ? 0 : arr.length; | |
let index = 0; | |
while(index < length) { | |
if (Array.isArray(arr[index])) { | |
flatDeep(arr[index], flatArr); | |
} else { | |
flattArr[flattenArr.length] = arr[index]; | |
} | |
index += 1; | |
} | |
return flatArr; | |
} | |
const flattenArray = (arr: Array<number>) => { | |
return flattenDeep(arr); | |
}; | |
export default flattenArray; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment