flatMap es-toolkit
Transforms each element of an array with a function's return value, then flattens to a specified depth and returns a new array.
Usage
flatMap(arr, iteratee, depth = 1)
Use flatMap when you want to transform and flatten an array simultaneously. First, it applies a function to each element, then flattens the resulting array to the specified depth.
It works identically to calling Array#flat from JavaScript with Array#map as map(iteratee).flat(depth), but it's faster.
You can flatten to various depths.
Parameters
The array to transform.
The function that transforms each array element. It receives the element, its index, and the array.
The depth to flatten. Default is 1.
Returns
Returns a new array where each element is transformed and flattened to the specified depth.
Examples
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.