I am trying to group by 2 columns in an array of objects and get a grouped sum total of a value. i have been using linq for this and this is the code:
import Enumerate from "linq";
let data = [
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
];
var result = Enumerate.from(data)
.groupBy((x) => x.Phase + ':'+ x.Task)
.select((x) => {
return {
Phase: x.first().Phase,
Task: x.first().Task,
Value: x.sum((y)=> y.Value | 0)
};
})
.toArray();
console.log (result);
and I get the expected result:
Array(4)[
0: Object { Phase: "Phase 1" ,Task: "Task 1" ,Value: 20 }
1: Object { Phase: "Phase 1" ,Task: "Task 2" ,Value: 30 }
2: Object { Phase: "Phase 2" ,Task: "Task 1" ,Value: 60 }
3: Object { Phase: "Phase 2" ,Task: "Task 2" ,Value: 70 }
length: 4
]
I struggled a bit to come to this result and had to use some workarounds like a string key in the groupBy and x.first() when selecting the data. My question is, is this the proper way to do it? Or am I missing something?
I'm not experienced with linq.js, but I'll give some insights based on my C# experience and what I can glean from the documentation.
It looks like there are additional arguments you can provide to the groupBy
function to make this more elegant. The arguments are:
So I think something like this should work:
var result = Enumerate.from(data)
.groupBy((x) => ({Phase: x.Phase, Task: x.Task}),
(x) => x.Value | 0,
(key, values) => ({
Phase: key.Phase,
Task: key.Task,
Value: values.sum()
}),
(key) => key.Phase + ':'+ key.Task)
)
.toArray();