Using .flatMap() with a callback that returns its argument unchanged is equivalent to calling .flat().
The .flat() method is more concise and clearly expresses the intent to flatten an array.
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.
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.
flatMap((
value: {
children: string[];
}
value) =>
value: {
children: string[];
}
value.
children: string[]
children);
const
const result:(number| {
extras:number[];
})[]
result =
const values: {
extras:number[];
}[]
values.
Array<{ extras: number[]; }>.flatMap<number | {
extras: number[];
}, undefined>(callback:(this:undefined, value: {
extras:number[];
}, index:number, array: {
extras:number[];
}[])=>number| {
extras:number[];
} |readonly (number| {
extras:number[];
})[], thisArg?:undefined): (number| {
extras:number[];
})[]
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.
If you are maintaining a codebase that must support environments where .flat() is not available, you may need to disable this rule.
Some older JavaScript environments do not support .flat() or .flatMap() and only polyfill .flatMap(), though this is rare in modern development.