flatMap.js 836 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 {Array|Function|Object|string} [iteratee=_.identity]
  14. * The function invoked per iteration.
  15. * @returns {Array} Returns the new flattened array.
  16. * @example
  17. *
  18. * function duplicate(n) {
  19. * return [n, n];
  20. * }
  21. *
  22. * _.flatMap([1, 2], duplicate);
  23. * // => [1, 1, 2, 2]
  24. */
  25. function flatMap(collection, iteratee) {
  26. return baseFlatten(map(collection, iteratee), 1);
  27. }
  28. module.exports = flatMap;