Groupby turtorial

Jul 22, 2013 at 7:33 PM
Hi,

I'm very confused about how to make something like this work using groupby. Let's say I have this pretty simple array of objects.
  var people = [
{ name: "Peter", age: 33, job: "Tech", salary:35000 },
{ name: "Bill", age: 42, job: "Tech", salary: 40000 },
{ name: "Jane", age: 35, job: "Nurse", salary: 65000 },
{ name: "Sally", age: 27, job: "Nurse", salary: 60000 },
{ name: "Ted", age: 31, job: "Janitor", salary: 30000 }
 ];
What I need to do is group each person by "job". Once they are grouped I need to total the salary for each job type. So for example people with job of "Tech" should show a combined total of 75000. "Nurses" should show a combined total of 125000 and "Janitors" should show a total of 30000.

A new object should be projected out with Just the Job name and the combined total for the job. Thanks in advance.
Jul 28, 2013 at 5:33 AM
Edited Jul 28, 2013 at 5:40 AM
This is my first attempt at using this library, and it appears the author(s) has done a good job of keeping with the LINQ structure. You should be able to adapt the examples in the LINQ documentation to a Javascript syntax.

For example, I worked with one of the GroupBy documentation pages and came up with this:
var grouped = Enumerable.From(people)
                        .GroupBy(function (person) { return person.job },     // Key selector
                                 function (person) {                          // Element selector
                                     return {
                                         Job: person.job,
                                         Salary: person.salary
                                     }
                                 },
                                 function (job, grouping) {                   // Result selector
                                     return {
                                         Job: job,
                                         TotalSalary: grouping.Sum(function (item) {
                                             return item.Salary
                                         })
                                     }
                                 })
                        .ToArray();
...which you can inspect with:
alert(JSON.stringify(grouped));
Be sure to only select a simple type in the "key selector". I started out by selecting a new JS object (with "Job" as the property name), and then quickly realized that the selector was comparing references rather than values. Selecting the simple string "person.job" results in string comparison rather than object comparison.

Cheers =)
Jul 29, 2013 at 3:07 AM
Thanks alot that example worked perfectly!!! Made things much clearer. Can't thank you enough. I have been scratching my head for days on that one. :)
Jul 18, 2014 at 2:42 AM
If you must group by multiple properties, you can always return JSON as the key selector:
var keySelector = function (person) { 
  return JSON.stringify({ job:person.job, location:person.location });
}
var grouped = Enumerable.From(people)
                    .GroupBy(keySelector, elementSelector, resultSelector)
                    .ToArray();