Skip to content

Instantly share code, notes, and snippets.

@SephReed
Created November 30, 2018 22:27
Show Gist options
  • Save SephReed/47db051195600bc0734fa7e68b8738d0 to your computer and use it in GitHub Desktop.
Save SephReed/47db051195600bc0734fa7e68b8738d0 to your computer and use it in GitHub Desktop.
interface ILayered extends Array<number | ILayered> { }
function flatten(flattenMe: ILayered) {
const flat: number[] = [];
const permeate = (layered: ILayered) => {
layered.forEach((item) => {
if(Array.isArray(item)) {
permeate(item);
} else {
flat.push(item);
}
})
}
permeate(flattenMe);
return flat;
}
@SephReed
Copy link
Author

Written in Typescript, uses recursive pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment