UNPKG

914 BJavaScriptView Raw
1import baseFlatten from './_baseFlatten';
2import map from './map';
3import toInteger from './toInteger';
4
5/**
6 * This method is like `_.flatMap` except that it recursively flattens the
7 * mapped results up to `depth` times.
8 *
9 * @static
10 * @memberOf _
11 * @since 4.7.0
12 * @category Collection
13 * @param {Array|Object} collection The collection to iterate over.
14 * @param {Array|Function|Object|string} [iteratee=_.identity]
15 * The function invoked per iteration.
16 * @param {number} [depth=1] The maximum recursion depth.
17 * @returns {Array} Returns the new flattened array.
18 * @example
19 *
20 * function duplicate(n) {
21 * return [[[n, n]]];
22 * }
23 *
24 * _.flatMapDepth([1, 2], duplicate, 2);
25 * // => [[1, 1], [2, 2]]
26 */
27function flatMapDepth(collection, iteratee, depth) {
28 depth = depth === undefined ? 1 : toInteger(depth);
29 return baseFlatten(map(collection, iteratee), depth);
30}
31
32export default flatMapDepth;