es-toolkit/docs/reference/array/flatMapDeep.md
Gromit (전민재) 7ad4ef15d3
feat(flatMapDeep): Add flatMapDeep (#464)
* 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>
2024-09-04 10:01:01 +09:00

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]