Created
November 21, 2024 01:50
-
-
Save ghostdevv/1ec0768384d068fc18fa9416de4539e6 to your computer and use it in GitHub Desktop.
flat array
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
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