es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
24 lines (23 loc) • 825 B
JavaScript
import { identity } from "../../function/identity.mjs";
import { isNil } from "../../predicate/isNil.mjs";
import { flattenDepth } from "./flattenDepth.mjs";
import { map } from "./map.mjs";
//#region src/compat/array/flatMap.ts
/**
* Creates a flattened array of values by running each element in collection through iteratee and flattening the mapped results.
*
* @template R
* @param collection - The collection to iterate over.
* @param [iteratee=identity] - The function invoked per iteration.
* @returns Returns the new flattened array.
*
* @example
* flatMap([1, 2], n => [n, n * 2]);
* // => [1, 2, 2, 4]
*/
function flatMap(collection, iteratee = identity) {
if (isNil(collection)) return [];
return flattenDepth(isNil(iteratee) ? map(collection) : map(collection, iteratee), 1);
}
//#endregion
export { flatMap };