I need to merge the objects together. The resource
property is what determines if the objects can be merged. To determine where the hours
property valu
Here's a version using the Ramda library (disclaimer: I'm one of the authors):
const process = pipe(
groupBy(prop('resource')),
values,
map(group => reduce((totals, member) => ({
name: member.name,
resource: member.resource,
totalHours: totals.totalHours + member.hours,
totalBillableHours: totals.totalBillableHours +
(member.billable ? member.hours : 0),
totalNonBillableHours: totals.totalNonBillableHours +
(member.billable ? 0 : member.hours)
}), head(group), group))
);
With this,
process(members)
yields
[
{
name: "Joe Smith",
resource: "00530000003mgYGAAY",
totalBillableHours: 20,
totalHours: 25,
totalNonBillableHours: 5
},
{
name: "Jam Smith",
resource: "00530000003mgYTAAY",
totalBillableHours: 14,
totalHours: 19,
totalNonBillableHours: 5
}
]
This works in two stages. First it collects the like values (using groupBy
) and extracts the results as an array (using values
).
Then it maps over the resulting list of groups, reducing each one to a single value by combining the fields as appropriate.
This might not be much of a help to you, as pulling in a library like Ramda for just one task is probably a ridiculous idea. But you might take inspiration in the breakdown of the problem.
Most of the Ramda functions used here are easy to create on your own and are quite useful for many purposes.
You can see this in action on the Ramda REPL.