docs: Add docs for flatMap
Some checks are pending
CI / codecov (push) Waiting to run
Release / release (push) Waiting to run

This commit is contained in:
raon0211 2024-07-15 22:50:07 +09:00
parent fec678cf34
commit 51e1970495
5 changed files with 44 additions and 3 deletions

View File

@ -69,6 +69,7 @@ function sidebar(): DefaultTheme.Sidebar {
},
{ text: "fill", link: "/zh_hans/reference/array/fill" },
{ text: "toFilled", link: "/zh_hans/reference/array/toFilled" },
{ text: "flatMap", link: "/reference/array/flatMap" },
{ text: "flatten", link: "/zh_hans/reference/array/flatten" },
{
text: "forEachRight",

View File

@ -8,7 +8,7 @@ JavaScript 언어에 포함된 [Array#flat](https://developer.mozilla.org/en-US/
```typescript
function flatMap<T, U, D extends number = 1>(
arr: readonly T[],
arr: T[],
iteratee: (item: T) => U,
depth?: D
): Array<FlatArray<U[], D>>;

View File

@ -8,7 +8,7 @@ It works the same as if you called [Array#flat](https://developer.mozilla.org/en
```typescript
function flatMap<T, U, D extends number = 1>(
arr: readonly T[],
arr: T[],
iteratee: (item: T) => U,
depth?: D
): Array<FlatArray<U[], D>>;

View File

@ -0,0 +1,40 @@
# flatMap
将嵌套数组的每个元素映射到给定的迭代函数,然后将其展平到所需的深度。
其工作方式与在 JavaScript 语言中调用 [Array#flat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) 和 [Array#map](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/map) 时相同,即 `map(iteratee).flat(depth)`,但速度更快。
## 签名
```typescript
function flatMap<T, U, D extends number = 1>(
arr: T[],
iteratee: (item: T) => U,
depth?: D
): Array<FlatArray<U[], D>>;
```
### 参数
- `arr` (`T[]`): 要展平的数组。
- `iteratee` (`T[]`): 映射每个数组元素的函数。
- `depth` (`D`): 要展平的深度,默认为 1。
### 返回值
(`Array<FlatArray<U[], D>>`): 一个新数组,其中每个元素都已映射并展平到所需的深度。
## 示例
```typescript
const array = [1, 2, 3];
const result1 = flatMap(array, item => [item, item], 1);
// 返回 [1, 1, 2, 2, 3, 3]
const result2 = flatMap(array, item => [[item, item]], 2);
// 返回 [1, 1, 2, 2, 3, 3]
const result3 = flatMap(array, item => [[[item, item]]], 3);
// 返回 [1, 1, 2, 2, 3, 3]
```