Search code examples
javascriptarraysgroupinglinq.js

linq.js Group By then Group By, How can it be achieved?


I want something that would result in this

[{
    firstName: "Sam",
    lastName: "Smith"
}, {
    firstName: "Sam",
    lastName: "Doe"
}, {
    firstName: "Sam",
    lastName: "Doe"
}, {
    firstName: "Sam",
    lastName: "Joe"
}]

Being something like this, with the exception of the number of columns to group with which is not fixed and the order of the columns as well

[{
    "Sam": [{
        "Smith": [{
            firstName: "Sam",
            lastName: "Smith"
        }]
    }, {
        "Doe": [{
            firstName: "Sam",
            lastName: "Doe"
        }, {
            firstName: "Sam",
            lastName: "Doe"
        }]
    }, {
        "Joe": [{
            firstName: "Sam",
            lastName: "Joe"
        }]
    }]
}]

I want to be able to build something like the following photo demonstrates

enter image description here

I tried several times but i can't get a grasp on it, please help me :).


Solution

  • I propose a different output structure. Your structure seems convenient at first glance, but in further processing it will prove unnecessarily difficult to handle.

    I would suggest this generic structure:

    [
      {
        key: "group 1",
        items: [{
          key: "group 1.1",
          items: [ {}, {}, {} ]
        }, {
          key: "group 1.2",
          items: [ {}, {} ]
        }]
      }, {
        key: "group 2",
        items: [{
          key: "group 2.1",
          items: [ {}, {}, [}, {} ]
        }, {
          key: "group 2.2",
          items: [ {} ]
        }]
      }
    ]
    

    You can create a structure like this relatively easily:

    var grouped = Enumerable.From(input).GroupBy("$.firstName", "", function(key, e) {
        return {
            key: key,
            items: e.GroupBy("$.lastName", "", function (key, e) {
                return {
                    key: key,
                    items: e.ToArray()
                };
            }).ToArray()
        };
    }).ToArray();
    

    when applied to this input:

    var input = [{
        firstName: "Sam",
        lastName: "Smith"
    }, {
        firstName: "Sam",
        lastName: "Doe"
    }, {
        firstName: "Sam",
        lastName: "Doe"
    }, {
        firstName: "Sam",
        lastName: "Joe"
    },{
        firstName: "John",
        lastName: "Joe"
    }];
    

    results in

    [
      {
        "key": "Sam",
        "items": [
          {
            "key": "Smith",
            "items": [
              {
                "firstName": "Sam",
                "lastName": "Smith"
              }
            ]
          },
          {
            "key": "Doe",
            "items": [
              {
                "firstName": "Sam",
                "lastName": "Doe"
              },
              {
                "firstName": "Sam",
                "lastName": "Doe"
              }
            ]
          },
          {
            "key": "Joe",
            "items": [
              {
                "firstName": "Sam",
                "lastName": "Joe"
              }
            ]
          }
        ]
      },
      {
        "key": "John",
        "items": [
          {
            "key": "Joe",
            "items": [
              {
                "firstName": "John",
                "lastName": "Joe"
              }
            ]
          }
        ]
      }
    ]
    

    Edit: To allow dynamically configurable grouping and ordering, a more advanced approach must be taken:

    // Enumerable, definition => Enumerable
    function applyOrder(items, definition) {
        var i, prop, prefix, suffix, orderFunc;
        if (!items) return;
        if (!definition) return items;
        for (i = 0; i < definition.length; i++) {
            // definition[i] is either "propertyName" or "propertyName DESC"
            prop = (definition[i] + " ").split(" ");
            prefix = i === 0 ? "OrderBy" : "ThenBy";
            suffix = prop[1].toUpperCase() === "DESC" ? "Descending" : "";
            orderFunc = prefix + suffix;
            items = items[orderFunc]("$." + prop[0]);
        }
        return items;
    }
    
    // Enumerable, definition => Enumerable
    function applyGroup(items, definition) {
        if (!items) return;
        if (!definition) return items;
        items = applyOrder(items, definition.order);
        if (!definition.group) return items;
        return items.GroupBy("$." + definition.group, "", function (key, e) {
            return {
                group: definition.group,
                key: key,
                items: applyGroup(e, definition.then).ToArray()
            };
        });
    }
    
    // Array, definition => Array
    function applyStructure(items, definition) {
        if (!items) return;
        if (!definition) return items;
        return applyGroup(Enumerable.From(items), definition).ToArray();
    }
    

    used like this:

    var result = applyStructure(companies, {
        group: "country",
        order: ["country"],
        then: {
            group: "city",
            order: ["city"],
            then: {
                order: ["companyName DESC"]
            }
        }
    });
    

    live at: http://jsfiddle.net/Tomalak/xth6ayuo/