mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 08:37:49 +00:00
Bump to v4.0.0.
This commit is contained in:
52
map.js
Normal file
52
map.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import arrayMap from './internal/arrayMap';
|
||||
import baseIteratee from './internal/baseIteratee';
|
||||
import baseMap from './internal/baseMap';
|
||||
import isArray from './isArray';
|
||||
|
||||
/**
|
||||
* Creates an array of values by running each element in `collection` through
|
||||
* `iteratee`. The iteratee is invoked with three arguments:
|
||||
* (value, index|key, collection).
|
||||
*
|
||||
* Many lodash methods are guarded to work as iteratees for methods like
|
||||
* `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`.
|
||||
*
|
||||
* The guarded methods are:
|
||||
* `ary`, `curry`, `curryRight`, `drop`, `dropRight`, `every`, `fill`,
|
||||
* `invert`, `parseInt`, `random`, `range`, `rangeRight`, `slice`, `some`,
|
||||
* `sortBy`, `take`, `takeRight`, `template`, `trim`, `trimEnd`, `trimStart`,
|
||||
* and `words`
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
|
||||
* @returns {Array} Returns the new mapped array.
|
||||
* @example
|
||||
*
|
||||
* function square(n) {
|
||||
* return n * n;
|
||||
* }
|
||||
*
|
||||
* _.map([1, 2], square);
|
||||
* // => [3, 6]
|
||||
*
|
||||
* _.map({ 'a': 1, 'b': 2 }, square);
|
||||
* // => [3, 6] (iteration order is not guaranteed)
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney' },
|
||||
* { 'user': 'fred' }
|
||||
* ];
|
||||
*
|
||||
* // using the `_.property` iteratee shorthand
|
||||
* _.map(users, 'user');
|
||||
* // => ['barney', 'fred']
|
||||
*/
|
||||
function map(collection, iteratee) {
|
||||
var func = isArray(collection) ? arrayMap : baseMap;
|
||||
return func(collection, baseIteratee(iteratee, 3));
|
||||
}
|
||||
|
||||
export default map;
|
||||
Reference in New Issue
Block a user