mirror of
https://github.com/toss/es-toolkit.git
synced 2024-12-02 12:52:23 +03:00
7ad4ef15d3
* feat: add flatMapDeep new function * refac: flatMapDeep refactor * fix: benchmark fix * fix: benchmark fix * Apply suggestions from code review --------- Co-authored-by: Sojin Park <raon0211@gmail.com> Co-authored-by: Sojin Park <raon0211@toss.im>
36 lines
1.1 KiB
Markdown
36 lines
1.1 KiB
Markdown
# flatMapDeep
|
|
|
|
Map each element of a nested array to the given iteratee function, then unpack and flatten all depths.
|
|
|
|
It works the same as if you called [Array#flat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) with [Array#map](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/map) as `map(iteratee).flat(Infinity)` in the JavaScript language, but it's faster.
|
|
|
|
## Signature
|
|
|
|
```typescript
|
|
function flattenDeep<T>(arr: T[]): Array<ExtractNestedArrayType<T>>;
|
|
```
|
|
|
|
### Parameters
|
|
|
|
- `arr` (`T[]`): The array to flatten.
|
|
- `iteratee` (`T[]`): A function that maps each array element.
|
|
|
|
### Returns
|
|
|
|
(`Array<ExtractNestedArrayType<T>>`): A new array with each element mapped and all depths flattened.
|
|
|
|
## Examples
|
|
|
|
```typescript
|
|
const array = [1, 2, 3];
|
|
|
|
const result1 = flatMapDeep(array, item => [item, item]);
|
|
// Return [1, 1, 2, 2, 3, 3]
|
|
|
|
const result2 = flatMapDeep(array, item => [[item, item]]);
|
|
// Return [1, 1, 2, 2, 3, 3]
|
|
|
|
const result3 = flatMapDeep(array, item => [[[item, item]]]);
|
|
// Return [1, 1, 2, 2, 3, 3]
|
|
```
|