linq.js Group By 那么Group By,如何实现呢?

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

我想要的东西会导致这个

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

是这样的,除了要分组的列数和列的顺序是不固定的

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

我希望能够构建如下图所示的东西

我试了好几次都搞不懂,请帮帮我:)。

我提出了一个不同的输出结构。你的结构乍一看似乎很方便,但在进一步处理中会证明不必要地难以处理。

我会建议这个通用结构:

[
  {
    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: [ {} ]
    }]
  }
]

您可以相对轻松地创建这样的结构:

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();

应用于此 input 时:

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

结果

[
  {
    "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"
          }
        ]
      }
    ]
  }
]

编辑:要允许动态配置分组和排序,必须采用更高级的方法:

// 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();
}

这样使用:

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

住在:http://jsfiddle.net/Tomalak/xth6ayuo/