flatMapDeep()
flatMapDeep<
T>(collection,iteratee):unknown[]
Creates a flattened array of values by running each element in collection thru iteratee and recursively flattening the mapped results.
DEPRECATED
Use array.flatMap().flat(Infinity) directly instead.
Type Parametersโ
T: Tโ
The type of elements in the collection.
Parametersโ
collection: readonly T[]โ
The collection to iterate over.
iteratee: (value, index, collection) => unknownโ
The function invoked per iteration.
Returns: unknown[]โ
The new flattened array.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
flatMapDeep (Lodash, es-toolkit) ยท โ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)
Exampleโ
// โ Deprecated approach
flatMapDeep([1, 2], n => [[n, n]]);
// => [1, 1, 2, 2]
// โ
Recommended approach
[1, 2].flatMap(n => [[n, n]]).flat(Infinity);
// => [1, 1, 2, 2]
How it works?โ
Maps and deeply flattens result.
Deprecated: Use flatMap() + flat(Infinity).
Native Equivalentโ
// โ flatMapDeep(arr, fn)
// โ
arr.flatMap(fn).flat(Infinity)
Use Casesโ
Map and deeply flatten ๐โ
Transform and flatten all nesting levels.
const data = [{ nested: [[1, 2], [3]] }, { nested: [[4]] }];
data.flatMap(d => d.nested).flat(Infinity);
// => [1, 2, 3, 4]
Extract deeply nestedโ
Collect all values from deep structures.
const tree = [{ children: [[a], [[b, c]]] }];
tree.flatMap(n => n.children).flat(Infinity);
Flatten recursive structureโ
Process hierarchical data into flat list.
const items = [{ sub: [[1], [[2, 3]]] }];
items.map(i => i.sub).flat(Infinity);
// => [1, 2, 3]