Skip to content

Instantly share code, notes, and snippets.

@ghostdevv
Created November 21, 2024 01:50
Show Gist options
  • Select an option

  • Save ghostdevv/1ec0768384d068fc18fa9416de4539e6 to your computer and use it in GitHub Desktop.

Select an option

Save ghostdevv/1ec0768384d068fc18fa9416de4539e6 to your computer and use it in GitHub Desktop.

Revisions

  1. ghostdevv created this gist Nov 21, 2024.
    5 changes: 5 additions & 0 deletions flat.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    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
    }