flatMapDepth.js 901 B

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