Last active
August 27, 2017 15:29
-
-
Save isaaclyman/beec34325141cc9eaa2d70311423bf95 to your computer and use it in GitHub Desktop.
(TypeScript/ES6) Exports function flattenArray, which takes a nested array of numbers and flattens it into a non-nested array of numbers
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
export interface recursiveArray extends Array<number | Array<any>> { } | |
export function flattenArray(arrayToFlatten: recursiveArray = []): Array<number> { | |
let workingArray: Array<number> = [] | |
for (const value of arrayToFlatten) { | |
if (Array.isArray(value)) { | |
workingArray = workingArray.concat(flattenArray(value)) | |
} else { | |
workingArray.push(value) | |
} | |
} | |
return workingArray | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment