linq.js Group By By Group By,如何实现?

时间:2015-01-03 10:08:23

标签: javascript arrays grouping linq.js

我想要一些会导致这个

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

我希望能够构建如下图所示的内容

enter image description here

我试了好几次但我无法掌握它,请帮助我:)。

1 个答案:

答案 0 :(得分:2)

我提出了不同的输出结构。乍看之下,您的结构看起来很方便,但在进一步处理过程中,它将难以处理。

我建议这个通用结构:

[
  {
    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/