mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
7ad4ef15d3
* feat: add flatMapDeep new function * refac: flatMapDeep refactor * fix: benchmark fix * fix: benchmark fix * Apply suggestions from code review --------- Co-authored-by: Sojin Park <raon0211@gmail.com> Co-authored-by: Sojin Park <raon0211@toss.im>
1.1 KiB
1.1 KiB
flatMapDeep
Map each element of a nested array to the given iteratee function, then unpack and flatten all depths.
It works the same as if you called Array#flat with Array#map as map(iteratee).flat(Infinity)
in the JavaScript language, but it's faster.
Signature
function flattenDeep<T>(arr: T[]): Array<ExtractNestedArrayType<T>>;
Parameters
arr
(T[]
): The array to flatten.iteratee
(T[]
): A function that maps each array element.
Returns
(Array<ExtractNestedArrayType<T>>
): A new array with each element mapped and all depths flattened.
Examples
const array = [1, 2, 3];
const result1 = flatMapDeep(array, item => [item, item]);
// Return [1, 1, 2, 2, 3, 3]
const result2 = flatMapDeep(array, item => [[item, item]]);
// Return [1, 1, 2, 2, 3, 3]
const result3 = flatMapDeep(array, item => [[[item, item]]]);
// Return [1, 1, 2, 2, 3, 3]