UNPKG

@walts81/linq-ts

Version:

Typescript/Javascript LINQ implementation library

20 lines (19 loc) 717 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.groupBy = void 0; const get_value_as_string_1 = require("./get-value-as-string"); Array.prototype.groupBy = groupBy; function groupBy(expression) { return this.reduce((prev, current, i) => { const tempKey = expression(current, i); const key = (0, get_value_as_string_1.getValueAsString)(tempKey); if (!prev.groups[key]) { const group = { key: tempKey, items: [] }; prev.groups[key] = group; prev.results.push(group); } prev.groups[key].items.push(current); return prev; }, { groups: {}, results: [] }).results; } exports.groupBy = groupBy;