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.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Split a string into substrings using the specified separator and return them as an array.
@param ― separator 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.
@param ― limit A value used to limit the number of elements returned in the array.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Calls a defined callback function on each element of an array. Then, flattens the result into
a new array.
This is identical to a map followed by flat with depth 1.
@param ― callback A function that accepts up to three arguments. The flatMap method calls the
callback function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callback function. If
thisArg is omitted, undefined is used as the this value.
Calls a defined callback function on each element of an array. Then, flattens the result into
a new array.
This is identical to a map followed by flat with depth 1.
@param ― callback A function that accepts up to three arguments. The flatMap method calls the
callback function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callback function. If
thisArg is omitted, undefined is used as the this value.
Split a string into substrings using the specified separator and return them as an array.
@param ― separator 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.
@param ― limit A value used to limit the number of elements returned in the array.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
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.