Javascript Array groupBy(selector, comparer)

Description

Javascript Array groupBy(selector, comparer)


Array.prototype.groupBy = function (selector, comparer) {
    var grp = [];
    var l = this.length;
    comparer = comparer || EqualityComparer;
    selector = selector || Selector;//from   w  w  w .j ava2s . co m

    for (var i = 0; i < l; i++) {
        var k = selector(this[i]);
        var g = grp.first(function (u) { return comparer(u.key, k); });

        if (!g) {
            g = [];
            g.key = k;
            grp.push(g);
        }

        g.push(this[i]);
    }
    return grp;
};



PreviousNext

Related