Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

invokeMap.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var apply = require('./_apply'),
  2. baseEach = require('./_baseEach'),
  3. baseInvoke = require('./_baseInvoke'),
  4. baseRest = require('./_baseRest'),
  5. isArrayLike = require('./isArrayLike');
  6. /**
  7. * Invokes the method at `path` of each element in `collection`, returning
  8. * an array of the results of each invoked method. Any additional arguments
  9. * are provided to each invoked method. If `path` is a function, it's invoked
  10. * for, and `this` bound to, each element in `collection`.
  11. *
  12. * @static
  13. * @memberOf _
  14. * @since 4.0.0
  15. * @category Collection
  16. * @param {Array|Object} collection The collection to iterate over.
  17. * @param {Array|Function|string} path The path of the method to invoke or
  18. * the function invoked per iteration.
  19. * @param {...*} [args] The arguments to invoke each method with.
  20. * @returns {Array} Returns the array of results.
  21. * @example
  22. *
  23. * _.invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
  24. * // => [[1, 5, 7], [1, 2, 3]]
  25. *
  26. * _.invokeMap([123, 456], String.prototype.split, '');
  27. * // => [['1', '2', '3'], ['4', '5', '6']]
  28. */
  29. var invokeMap = baseRest(function(collection, path, args) {
  30. var index = -1,
  31. isFunc = typeof path == 'function',
  32. result = isArrayLike(collection) ? Array(collection.length) : [];
  33. baseEach(collection, function(value) {
  34. result[++index] = isFunc ? apply(path, value, args) : baseInvoke(value, path, args);
  35. });
  36. return result;
  37. });
  38. module.exports = invokeMap;