You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
955 B
28 lines
955 B
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var tslib_1 = require("tslib");
|
|
var is_array_1 = tslib_1.__importDefault(require("./is-array"));
|
|
var is_function_1 = tslib_1.__importDefault(require("./is-function"));
|
|
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
function groupBy(data, condition) {
|
|
if (!condition || !(0, is_array_1.default)(data)) {
|
|
return {};
|
|
}
|
|
var result = {};
|
|
// 兼容方法和 字符串的写法
|
|
var predicate = (0, is_function_1.default)(condition) ? condition : function (item) { return item[condition]; };
|
|
var key;
|
|
for (var i = 0; i < data.length; i++) {
|
|
var item = data[i];
|
|
key = predicate(item);
|
|
if (hasOwnProperty.call(result, key)) {
|
|
result[key].push(item);
|
|
}
|
|
else {
|
|
result[key] = [item];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
exports.default = groupBy;
|
|
//# sourceMappingURL=group-by.js.map
|