-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
array-group-by.js
32 lines (30 loc) · 1.2 KB
/
array-group-by.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var bind = require('../internals/function-bind-context');
var has = require('../internals/has');
var IndexedObject = require('../internals/indexed-object');
var toObject = require('../internals/to-object');
var toLength = require('../internals/to-length');
var toPropertyKey = require('../internals/to-property-key');
var objectCreate = require('../internals/object-create');
var arrayFromConstructorAndList = require('../internals/array-from-constructor-and-list');
var push = [].push;
module.exports = function ($this, callbackfn, that, specificConstructor) {
var O = toObject($this);
var self = IndexedObject(O);
var boundFunction = bind(callbackfn, that, 3);
var target = objectCreate(null);
var length = toLength(self.length);
var index = 0;
var Constructor, key, value;
for (;length > index; index++) {
value = self[index];
key = toPropertyKey(boundFunction(value, index, O));
if (has(target, key)) push.call(target[key], value);
else target[key] = [value];
}
if (specificConstructor) {
Constructor = specificConstructor(O);
if (Constructor !== Array) {
for (key in target) target[key] = arrayFromConstructorAndList(Constructor, target[key]);
}
} return target;
};