Created
February 18, 2019 23:23
-
-
Save richistron/8d6205db7b9b4c2e8f85ef5aa5c7a5af to your computer and use it in GitHub Desktop.
flats an 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
type arr = number[]; | |
type multiple = number[][]; | |
type irregular = any[]; | |
const flatten = (arr : arr | multiple | irregular) : number[] => { | |
const newState : number[] = []; | |
for (let item of arr) { | |
if (Array.isArray(item)) { | |
flatten(item).forEach(val => { | |
newState.push(val); | |
}); | |
} | |
else { | |
newState.push(item); | |
} | |
} | |
return newState; | |
}; | |
export default flatten; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment