Skip to content

Instantly share code, notes, and snippets.

@isaaclyman
Last active August 27, 2017 15:29
Show Gist options
  • Save isaaclyman/beec34325141cc9eaa2d70311423bf95 to your computer and use it in GitHub Desktop.
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
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