UNPKG

965 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var is_array_1 = (0, tslib_1.__importDefault)(require("./is-array"));
5var is_function_1 = (0, tslib_1.__importDefault)(require("./is-function"));
6var hasOwnProperty = Object.prototype.hasOwnProperty;
7function groupBy(data, condition) {
8 if (!condition || !(0, is_array_1.default)(data)) {
9 return {};
10 }
11 var result = {};
12 // 兼容方法和 字符串的写法
13 var predicate = (0, is_function_1.default)(condition) ? condition : function (item) { return item[condition]; };
14 var key;
15 for (var i = 0; i < data.length; i++) {
16 var item = data[i];
17 key = predicate(item);
18 if (hasOwnProperty.call(result, key)) {
19 result[key].push(item);
20 }
21 else {
22 result[key] = [item];
23 }
24 }
25 return result;
26}
27exports.default = groupBy;
28//# sourceMappingURL=group-by.js.map
\No newline at end of file