Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var createAggregator = require('../internal/createAggregator');
2
3
/**
4
* Creates an object composed of keys generated from the results of running
5
* each element of `collection` through `iteratee`. The corresponding value
6
* of each key is the last element responsible for generating the key. The
7
* iteratee function is bound to `thisArg` and invoked with three arguments:
8
* (value, index|key, collection).
9
*
10
* If a property name is provided for `iteratee` the created `_.property`
11
* style callback returns the property value of the given element.
12
*
13
* If a value is also provided for `thisArg` the created `_.matchesProperty`
14
* style callback returns `true` for elements that have a matching property
15
* value, else `false`.
16
*
17
* If an object is provided for `iteratee` the created `_.matches` style
18
* callback returns `true` for elements that have the properties of the given
19
* object, else `false`.
20
*
21
* @static
22
* @memberOf _
23
* @category Collection
24
* @param {Array|Object|string} collection The collection to iterate over.
25
* @param {Function|Object|string} [iteratee=_.identity] The function invoked
26
* per iteration.
27
* @param {*} [thisArg] The `this` binding of `iteratee`.
28
* @returns {Object} Returns the composed aggregate object.
29
* @example
30
*
31
* var keyData = [
32
* { 'dir': 'left', 'code': 97 },
33
* { 'dir': 'right', 'code': 100 }
34
* ];
35
*
36
* _.indexBy(keyData, 'dir');
37
* // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
38
*
39
* _.indexBy(keyData, function(object) {
40
* return String.fromCharCode(object.code);
41
* });
42
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
43
*
44
* _.indexBy(keyData, function(object) {
45
* return this.fromCharCode(object.code);
46
* }, String);
47
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
48
*/
49
var indexBy = createAggregator(function(result, value, key) {
50
result[key] = value;
51
});
52
53
module.exports = indexBy;
54
55