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]