rsuite
Version:
A suite of react components
31 lines (27 loc) • 720 B
JavaScript
import { flattenTree } from '../utils/treeUtils';
export default function getDataGroupBy(data, key, sort) {
if (data === void 0) {
data = [];
}
var tempData = {};
var isSort = typeof sort === 'function';
data.forEach(function (item) {
if (!tempData[item[key]]) {
tempData[item[key]] = [];
}
tempData[item[key]].push(item);
});
var nextData = Object.entries(tempData).map(function (_ref) {
var groupTitle = _ref[0],
children = _ref[1];
return {
groupTitle: groupTitle,
group: true,
children: isSort ? children.sort(sort(false)) : children
};
});
if (isSort) {
nextData = nextData.sort(sort(true));
}
return flattenTree(nextData);
}