Linq.js:按两个属性(字段)分组

Linq.js : Group By two properties (fields)

本文关键字:两个 字段 分组 属性 js Linq      更新时间:2023-09-26

我有以下数组:

var source = [
            { "DistributorId": 1, "DistributorName": "Distributor 01", "PriceListId": 1, "Year": 2014, "Month": 9 },
            { "DistributorId": 1, "DistributorName": "Distributor 01", "PriceListId": 2, "Year": 2014, "Month": 10 },
            { "DistributorId": 2, "DistributorName": "Distributor 02", "PriceListId": 3, "Year": 2014, "Month": 10 },
            { "DistributorId": 3, "DistributorName": "Distributor 03", "PriceListId": 4, "Year": 2014, "Month": 9 },
            { "DistributorId": 3, "DistributorName": "Distributor 03", "PriceListId": 5, "Year": 2014, "Month": 10 }
        ];

我想使用linq.js将这些数组按两个字段"DistributorId"answers"DistributorName"分组以获得以下结果:

var des =
        [
            {
                "DistributorId": 1,
                "DistributorName": "Distributor 01",
                "PriceLists":
                [
                    { "Year": 2014, "Month": 9 },
                    { "Year": 2014, "Month": 10 }
                ]
            },
            {
                "DistributorId": 2,
                "DistributorName": "Distributor 02",
                "PriceLists":
                [
                    { "Year": 2014, "Month": 10 }
                ]
            },
            {
                "DistributorId": 3,
                "DistributorName": "Distributor 03",
                "PriceLists":
                [
                    { "Year": 2014, "Month": 9 },
                    { "Year": 2014, "Month": 10 }
                ]
            }
        ];

您想要的group by过载如下所示:

// Overload:function(keySelector,elementSelector,resultSelector,compareSelector)

首先,您需要一个密钥,它是分发服务器id和名称的组合。然后收集具有相同分销商id和名称的所有年份和月份。当然,结果和比较关键对象的方法(作为字符串的属性是实现这一点的简单方法)。

var query = Enumerable.from(data)
    .groupBy(
        "{ Id: $.DistributorId, Name: $.DistributorName }",
        "{ Year: $.Year, Month: $.Month }",
        "{ DistributorId: $.Id, DistributorName: $.Name, PriceLists: $$.toArray() }",
        "String($.Id) + $.Name"
    )
    .toArray();

请注意,我使用的是linq.js3.x名称:使用lowerCamelCase的方法。旧版本更改为UpperCamelCase。

这应该可以做到:

var source = [
    { "DistributorId": 1, "DistributorName": "Distributor 01", "PriceListId": 1, "Year": 2014, "Month": 9 },
    { "DistributorId": 1, "DistributorName": "Distributor 01", "PriceListId": 2, "Year": 2014, "Month": 10 },
    { "DistributorId": 2, "DistributorName": "Distributor 02", "PriceListId": 3, "Year": 2014, "Month": 10 },
    { "DistributorId": 3, "DistributorName": "Distributor 03", "PriceListId": 4, "Year": 2014, "Month": 9 },
    { "DistributorId": 3, "DistributorName": "Distributor 03", "PriceListId": 5, "Year": 2014, "Month": 10 }
];
var dest = [];
source.map(function(current){
    var id = current.DistributorId - 1;
    dest[id] = dest[id] || {
        "DistributorId": current.DistributorId,
        "DistributorName": current.DistributorName,
        "PriceLists": []
    };
    dest[id].PriceLists.push({ "Year": current.Year, "Month": current.Month });
})