Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var arrayMap = require('../internal/arrayMap'),
2
baseCallback = require('../internal/baseCallback'),
3
baseMap = require('../internal/baseMap'),
4
isArray = require('../lang/isArray');
5
6
/**
7
* Creates an array of values by running each element in `collection` through
8
* `iteratee`. The `iteratee` is bound to `thisArg` and invoked with three
9
* arguments: (value, index|key, collection).
10
*
11
* If a property name is provided for `iteratee` the created `_.property`
12
* style callback returns the property value of the given element.
13
*
14
* If a value is also provided for `thisArg` the created `_.matchesProperty`
15
* style callback returns `true` for elements that have a matching property
16
* value, else `false`.
17
*
18
* If an object is provided for `iteratee` the created `_.matches` style
19
* callback returns `true` for elements that have the properties of the given
20
* object, else `false`.
21
*
22
* Many lodash methods are guarded to work as iteratees for methods like
23
* `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`.
24
*
25
* The guarded methods are:
26
* `ary`, `callback`, `chunk`, `clone`, `create`, `curry`, `curryRight`,
27
* `drop`, `dropRight`, `every`, `fill`, `flatten`, `invert`, `max`, `min`,
28
* `parseInt`, `slice`, `sortBy`, `take`, `takeRight`, `template`, `trim`,
29
* `trimLeft`, `trimRight`, `trunc`, `random`, `range`, `sample`, `some`,
30
* `sum`, `uniq`, and `words`
31
*
32
* @static
33
* @memberOf _
34
* @alias collect
35
* @category Collection
36
* @param {Array|Object|string} collection The collection to iterate over.
37
* @param {Function|Object|string} [iteratee=_.identity] The function invoked
38
* per iteration.
39
* @param {*} [thisArg] The `this` binding of `iteratee`.
40
* @returns {Array} Returns the new mapped array.
41
* @example
42
*
43
* function timesThree(n) {
44
* return n * 3;
45
* }
46
*
47
* _.map([1, 2], timesThree);
48
* // => [3, 6]
49
*
50
* _.map({ 'a': 1, 'b': 2 }, timesThree);
51
* // => [3, 6] (iteration order is not guaranteed)
52
*
53
* var users = [
54
* { 'user': 'barney' },
55
* { 'user': 'fred' }
56
* ];
57
*
58
* // using the `_.property` callback shorthand
59
* _.map(users, 'user');
60
* // => ['barney', 'fred']
61
*/
62
function map(collection, iteratee, thisArg) {
63
var func = isArray(collection) ? arrayMap : baseMap;
64
iteratee = baseCallback(iteratee, thisArg, 3);
65
return func(collection, iteratee);
66
}
67
68
module.exports = map;
69
70