Linq.js Group By the time Group By, how can this be achieved?

I want something that will lead to this

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

      

Be something similar, except for the number of columns the group is not grouped into, and the order of the columns.

[{
    "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 create something like the following photo demonstrates

enter image description here

I have tried several times but I cannot figure it out, please help me :).

+3


source to share


1 answer


I suggest a different output structure. Your structure looks convenient at first glance, but it will be unnecessarily difficult to process when further processed.

I would suggest this general 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"
}];

      

leads to



[
  {
    "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 custom grouping and ordering, a more advanced approach needs to 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/

+2


source







All Articles