javascript linq.js groupby [英] javascript linq.js groupby

查看:244
本文介绍了javascript linq.js groupby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript函数中使用了以下JSON数据(这里简化了它):

  var data = [
{到期日:2013-01-02T00:00:00,结束日期:2013-01-16T00:00:00,类别:10,金额:14.53},
{到期日:2013-01 -02T00:00:00,结束日期:2013-01-16T00:00:00,类别:25,金额:14.86},
{到期:2013-02-06T00:00:00, EndDate:2013-02-20T00:00:00,类别:10,金额:14.43},
{到期日:2013-02-06T00:00:00,结束日期:2013-02-20T00 :00:00,类别:25,金额:14.76}
];

注意:对于任何特定的到期值,EndDate值始终相同。



现在理想情况下,我希望在结尾处具有以下JSON(按到期日分组,并注意属性名称中使用Category值):

  [
{
到期日期:2013/1/2,(注意:不会对日期格式产生影响)
EndDate :2013/1/16,
Category10:14.53,
Category25:14.86
},
{
到期日:2013/2/6,
EndDate :2013/2/20,
Category10:14.43,
Category25:14.76
},
]

但是我不确定我会一口气完成所有事情,因此可以在每个日期对下面列出一系列类别和金额,然后我可以通过以下方式进行处理:

  [
{
到期日:2013/1/2,
结束日期:2013 / 1/16,
类别:[{10,25}],
金额:[{ 14.53,14.86}]
},
{
到期:2013/2/6,
结束日期:2013/2/20,
类别:[{ 25}],
金额:[{14.43,14.76}]
},
]

我试图在linq.js库中使用groupby函数来获取必要的JSON,但无法获得我需要的内容。

<

使用以下内容:

  var result = linq.groupBy($。Expiry,,k,e => {Expiry:k,Categories:e.select('$。Category')。toArray(),Amounts:e.select('$。Amount')。toArray()})。toArray(); 

我得到:

 <$ c $ [
{
到期:2013/1/2,
类别:[{10,25}],
金额:[{14.53,14.86}]
},
{
到期:2013/2/6,
类别:[{10,25}],
金额:[{14.43,14.76}]
},
]

但是我无法弄清楚如何获取EndDate。 ..

任何人都可以协助?
$ b $ Ta

Expiry 和 EndDate 然后你可以将它包含在你的结果中。



我不建议试图获得理想的结果,创建类似这样的结果并不符合LINQ是做什么的,相反,你应该收集你正在查询的项目。

  var query = Enumerable.From(data)
.GroupBy(
{Expiry:$ .Expiry,EndDate: $ .EndDate},
null,
{Expiry:$ .Expiry,EndDate:$ .EndDate,Categories:$$。Select('$。Category')。ToArray(),Amounts: $$。Select('$。Amount')。ToArray()},
$ .Expiry +' - '+ $ .EndDate//比较选择器需要

。 ToArray的();

然而,仍然有可能获得理想的结果,但我仍然建议不要使用这种方法。

  var query = Enumerable.From(data)
.GroupBy(
{Expiry:$ .Expiry,EndDate:$ .EndDate},
null,
function(key,g){
return g.Aggregate({
Expiry:key.Expiry,
EndDate:key.EndDate
},function(result,item){
result ['Category'+ item.Category] ​​= item.Amount;
返回结果;
});
},
$ .Expiry +' - '+ $ .EndDate

.ToArray();


I have the following JSON data in a Javascript function (it's simplified here):

var data = [
  { Expiry: "2013-01-02T00:00:00", EndDate: "2013-01-16T00:00:00", Category: 10, Amount: 14.53 },
  { Expiry: "2013-01-02T00:00:00", EndDate: "2013-01-16T00:00:00", Category: 25, Amount: 14.86 },
  { Expiry: "2013-02-06T00:00:00", EndDate: "2013-02-20T00:00:00", Category: 10, Amount: 14.43 },
  { Expiry: "2013-02-06T00:00:00", EndDate: "2013-02-20T00:00:00", Category: 25, Amount: 14.76 }
];

Note: An EndDate value will always be the same for any specific Expiry value.

Now ideally I'd like to have the following JSON at the end (grouped by Expiry date and note the use of the Category value in the property name):

[
   {
     Expiry: 2013/1/2,     (note: not fussed about the date format)
     EndDate: 2013/1/16,
     Category10: 14.53,
     Category25: 14.86
   },
   {
     Expiry: 2013/2/6,
     EndDate: 2013/2/20,     
     Category10: 14.43,
     Category25: 14.76
   },
]

But not sure I'll get that all in one go so can settle for an array of Categories and Amounts under each date pair that I can then deal with to get the above somehow:

[
   {
     Expiry: 2013/1/2,    
     EndDate: 2013/1/16,
     Categories: [ { 10, 25 } ],
     Amounts: [ { 14.53, 14.86 } ]
   },
   {
     Expiry: 2013/2/6,
     EndDate: 2013/2/20,
     Categories: [ { 10, 25 } ],
     Amounts: [ { 14.43, 14.76 } ]
   },
]

I'm trying to use the groupby function in the linq.js library to get the necessary JSON but can't get to what I need.

With the following:

var result = linq.groupBy("$.Expiry", "", "k,e => { Expiry: k, Categories: e.select('$.Category').toArray(), Amounts: e.select('$.Amount').toArray()}").toArray();

I get:

[
   {
     Expiry: 2013/1/2,    
     Categories: [ { 10, 25 } ],
     Amounts: [ { 14.53, 14.86 } ]
   },
   {
     Expiry: 2013/2/6,
     Categories: [ { 10, 25 } ],
     Amounts: [ { 14.43, 14.76 } ]
   },
]

But I can't workout how to get the EndDate in...

Can anyone assist?

Ta,

解决方案

Your key should be a composite key with both the Expiry and EndDate. Then you can include it in your result.

I wouldn't recommend trying to get your ideal result, creating results like that aren't in the spirit of what LINQ was made to do. Rather, you should collect items you are querying for.

var query = Enumerable.From(data)
    .GroupBy(
        "{ Expiry: $.Expiry, EndDate: $.EndDate }",
        null,
        "{ Expiry: $.Expiry, EndDate: $.EndDate, Categories: $$.Select('$.Category').ToArray(), Amounts: $$.Select('$.Amount').ToArray() }",
        "$.Expiry + '-' + $.EndDate" // compare selector needed
    )
    .ToArray();

However it is still possible to get your ideal result, but again, I would recommend not using this approach.

var query = Enumerable.From(data)
    .GroupBy(
        "{ Expiry: $.Expiry, EndDate: $.EndDate }",
        null,
        function (key, g) {
            return g.Aggregate({
                Expiry: key.Expiry,
                EndDate: key.EndDate
            }, function (result, item) {
                result['Category' + item.Category] = item.Amount;
                return result;
            });
        },
        "$.Expiry + '-' + $.EndDate"
    )
    .ToArray();

这篇关于javascript linq.js groupby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆