Skip to content

Instantly share code, notes, and snippets.

@richistron
Created February 18, 2019 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richistron/8d6205db7b9b4c2e8f85ef5aa5c7a5af to your computer and use it in GitHub Desktop.
Save richistron/8d6205db7b9b4c2e8f85ef5aa5c7a5af to your computer and use it in GitHub Desktop.
flats an array of numbers
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