Egor Malkevich Egor Malkevich - 1 year ago 165
Javascript Question

How to do group by filter in Aurelia

I'm looking way to do something like


$scope.players = [
{name: 'Gene', team: 'alpha'},
{name: 'George', team: 'beta'},
{name: 'Steve', team: 'gamma'},
{name: 'Paula', team: 'beta'},
{name: 'Scruath', team: 'gamma'}


<ul repeat.for="obj of players | groupBy: 'team'">
Group name: ${}
<li repeat.for="player of obj.values">
player: ${}

Is it possible to do? Or what the better way to do this logic in Aurelia way?

Leo Leo
Answer Source

You can do this using a ValueConverter.

export class GroupByValueConverter {
    toView(array, groupBy) {

        var groups = {};

        array.forEach(function (o) {
            var group = o[groupBy];
            groups[group] = groups[group] || [];

        return Object.keys(groups).map(function (group) {
            return {
                group: group,
                values: groups[group]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download