Skip to content

Instantly share code, notes, and snippets.

@ghostdevv
Created November 21, 2024 01:50
Show Gist options
  • Save ghostdevv/1ec0768384d068fc18fa9416de4539e6 to your computer and use it in GitHub Desktop.
Save ghostdevv/1ec0768384d068fc18fa9416de4539e6 to your computer and use it in GitHub Desktop.
flat array
function flat<T extends any[], D extends number = 1>(array: T, depth?: D): FlatArray<T, D>[] {
return typeof depth == 'undefined' || depth > 0
? array.reduce((a, x) => a.concat(Array.isArray(x) ? flat(x, (depth ?? 1) - 1) : x), [])
: array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment