2026-05-31 13:54:31 +02:00

21 lines
910 B
JavaScript

import { flattenDeep } from "./flattenDeep.mjs";
//#region src/array/flatMapDeep.ts
/**
* Recursively maps each element in an array using a provided iteratee function and then deeply flattens the resulting array.
*
* @template T - The type of elements within the array.
* @template U - The type of elements within the returned array from the iteratee function.
* @param {T[]} arr - The array to flatten.
* @param {(item: T, index: number, array: readonly T[]) => U} iteratee - The function that produces the new array elements. It receives the element, its index, and the array.
* @returns {Array<ExtractNestedArrayType<U>>} A new array that has been flattened.
*
* @example
* const result = flatMapDeep([1, 2, 3], n => [[n, n]]);
* // [1, 1, 2, 2, 3, 3]
*/
function flatMapDeep(arr, iteratee) {
return flattenDeep(arr.map((item, index) => iteratee(item, index, arr)));
}
//#endregion
export { flatMapDeep };