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