【问题标题】:Linq.js : Group By two properties (fields)Linq.js:按两个属性(字段)分组
【发布时间】:2014-09-29 08:20:28
【问题描述】:

我有以下数组:

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”和“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 }
                ]
            }
        ];

【问题讨论】:

    标签: javascript group-by linq.js


    【解决方案1】:

    您想要的 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.js 3.x 名称:使用 lowerCamelCase 的方法。旧版本更改为 UpperCamelCase。

    【讨论】:

    • 它有效,谢谢 - 您在使用 linq.js 3.x 时是否遇到任何问题,因为它仍处于测试阶段?
    • 我不能说我有。大多数情况下都是一样的。我并没有真正使用添加的新方法,但我没有理由认为它们不起作用。
    • 如果我想返回计数以使我的 ToArray 返回以下对象数组怎么办 [{ "DistributorId": 1, "DistributorName": "Distributor 01", Total: 2} { "DistributorId ": 1, "DistributorName": "Distributor 02", 总数: 1} { "DistributorId": 1, "DistributorName": "Distributor 03", 总数: 2} ]
    【解决方案2】:

    这应该可以解决问题:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      • 2023-03-14
      • 1970-01-01
      • 2014-07-05
      • 2013-03-19
      相关资源
      最近更新 更多