Skip to content

arrayFlatMapMethods

Reports using .map().flat() when .flatMap() can be used.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

Array.prototype.flatMap() combines mapping and flattening in a single step. Using .map().flat() creates an intermediate array that is immediately discarded, which is less efficient. This rule reports when .map().flat() can be replaced with .flatMap().

The rule only applies when .flat() is called with no arguments or with a depth of 1, since .flatMap() always flattens to a depth of one level.

declare const
const array: number[]
array
: number[];
const array: number[]
array
.
Array<number>.map<number[]>(callbackfn: (value: number, index: number, array: number[]) => number[], thisArg?: any): number[][]

Calls a defined callback function on each element of an array, and returns an array that contains the results.

@paramcallbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

map
((
value: number
value
) => [
value: number
value
,
value: number
value
* 2]).
Array<number[]>.flat<number[][], 1>(this: number[][], depth?: 1 | undefined): number[]

Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.

@paramdepth The maximum recursion depth

flat
();
declare const
const strings: string[]
strings
: string[];
const strings: string[]
strings
.
Array<string>.map<string[]>(callbackfn: (value: string, index: number, array: string[]) => string[], thisArg?: any): string[][]

Calls a defined callback function on each element of an array, and returns an array that contains the results.

@paramcallbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

map
((
value: string
value
) =>
value: string
value
.
String.split(separator: string | RegExp, limit?: number): string[] (+1 overload)

Split a string into substrings using the specified separator and return them as an array.

@paramseparator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.

@paramlimit A value used to limit the number of elements returned in the array.

split
(",")).
Array<string[]>.flat<string[][], 1>(this: string[][], depth?: 1 | undefined): string[]

Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.

@paramdepth The maximum recursion depth

flat
();
declare const
const array: number[]
array
: number[];
const array: number[]
array
.
Array<number>.map<number[]>(callbackfn: (value: number, index: number, array: number[]) => number[], thisArg?: any): number[][]

Calls a defined callback function on each element of an array, and returns an array that contains the results.

@paramcallbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

map
((
value: number
value
) => [
value: number
value
]).
Array<number[]>.flat<number[][], 1>(this: number[][], depth?: 1 | undefined): number[]

Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.

@paramdepth The maximum recursion depth

flat
(1);

This rule is not configurable.

If you prefer the explicit two-step approach of .map().flat() for readability, or if your codebase has a large number of existing uses that would be difficult to refactor, you may disable this rule.

Made with ❤️‍🔥 around the world by the Flint team and contributors.