flatMap.js 812 B

123456789101112131415161718192021222324252627282930
  1. var baseFlatten = require('./_baseFlatten'),
  2. map = require('./map');
  3. /**
  4. * Creates a flattened array of values by running each element in `collection`
  5. * thru `iteratee` and flattening the mapped results. The iteratee is invoked
  6. * with three arguments: (value, index|key, collection).
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 4.0.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. * @returns {Array} Returns the new flattened array.
  15. * @example
  16. *
  17. * function duplicate(n) {
  18. * return [n, n];
  19. * }
  20. *
  21. * _.flatMap([1, 2], duplicate);
  22. * // => [1, 1, 2, 2]
  23. */
  24. function flatMap(collection, iteratee) {
  25. return baseFlatten(map(collection, iteratee), 1);
  26. }
  27. module.exports = flatMap;