javascript - Linq.Js Group By with Count - Stack Overflow

I have the following array:var data=[{ "Id": 1, "Name": "NameOne"}{ &qu

I have the following array:

var data=  [{ "Id": 1, "Name": "NameOne"}
            { "Id": 2, "Name": "NameTwo"}
            { "Id": 2, "Name": "NameTwo"}]
            { "Id": 3, "Name": "NameThree"}]

Using linq.js I would like to return the following array:

var data=  [{ "Id": 1, "Name": "NameOne", Total: 1}
            { "Id": 2, "Name": "NameTwo", Total: 2}
            { "Id": 3, "Name": "NameThree", Total: 1}]

This means that I need to use GroupBy() with a Count(). I am not sure how to apply this using the linq.js reference.

I have the following array:

var data=  [{ "Id": 1, "Name": "NameOne"}
            { "Id": 2, "Name": "NameTwo"}
            { "Id": 2, "Name": "NameTwo"}]
            { "Id": 3, "Name": "NameThree"}]

Using linq.js I would like to return the following array:

var data=  [{ "Id": 1, "Name": "NameOne", Total: 1}
            { "Id": 2, "Name": "NameTwo", Total: 2}
            { "Id": 3, "Name": "NameThree", Total: 1}]

This means that I need to use GroupBy() with a Count(). I am not sure how to apply this using the linq.js reference.

Share Improve this question edited Jan 28, 2015 at 16:23 J. Steen 15.6k15 gold badges58 silver badges64 bronze badges asked Jan 28, 2015 at 16:17 navnav 5094 silver badges19 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

It's simple really:

var data = [
    { Id: 1, Name: 'NameOne' },
    { Id: 2, Name: 'NameTwo' },
    { Id: 2, Name: 'NameTwo' },
    { Id: 3, Name: 'NameThree' }
];
var query = Enumerable.From(data)
    // GroupBy (keySelector, elementSelector, resultSelector, pareSelector)
    .GroupBy(
        null, // (identity)
        null, // (identity)
        "{ Id: $.Id, Name: $.Name, Total: $$.Count() }",
        "'' + $.Id + '-' + $.Name"
    )
    .ToArray();

Use the overload of GroupBy() that includes the resultSelector, you'll want to grab the count of the grouped items (the second parameter).

You were probably having issues with the data not being uniform. a reduce flattens your data structure, and then you can manipulate it as you wish in the .Select().

var intialData = [[{ "Id": 1, "Name": "NameOne"}, { "Id": 2, "Name": "NameTwo"}, { "Id": 2, "Name": "NameTwo"}], { "Id": 3, "Name": "NameThree"}];         

var data = Enumerable.From(intialData.reduce(function(a,b) { return a.concat(b); }))
                     .GroupBy(function(item) { return item.Id; })
                     .Select(function(item) { return {"Id":item.source[0].Id, "Name":item.source[0].Name, "Total": item.source.length}; })
                     .ToArray();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744239715a4564645.html

相关推荐

  • javascript - Linq.Js Group By with Count - Stack Overflow

    I have the following array:var data=[{ "Id": 1, "Name": "NameOne"}{ &qu

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信