0
var Employees = [
    {
        "id": "382740",
        "PayrollID": "8117817425",
        "EmployeeName": "Bob Jones",
        "StartTime": "15:15:00.0000000",
        "FinishTime": "18:15:00.0000000",
        "BreakTime": "45",
        "TotalTime": 2,
        "Comments": "Test",
        "Rate": "19"
    },
    {
        "id": "439617",
        "PayrollID": "8117817425",
        "EmployeeName": "Peter Pan",
        "StartTime": "16:15:00.0000000",
        "FinishTime": "21:15:00.0000000",
        "BreakTime": "60",
        "TotalTime": 4,
        "Comments": "Test",
        "Rate": "32"
    },
    {
        "id": "201636",
        "PayrollID": "5042289623",
        "EmployeeName": "Bob Jones",
        "StartTime": "09:56:00.0000000",
        "FinishTime": "11:56:00.0000000",
        "BreakTime": "45",
        "TotalTime": 1.25,
        "Comments": "Test Comments",
        "Rate": "19"
    },
    {
        "id": "799653",
        "PayrollID": "5042289623",
        "EmployeeName": "Clarke Kent",
        "StartTime": "16:49:00.0000000",
        "FinishTime": "21:49:00.0000000",
        "BreakTime": "60",
        "TotalTime": 4,
        "Comments": "Test",
        "Rate": "19"
    },
    {
        "id": "951567",
        "PayrollID": "5042289623",
        "EmployeeName": "Bob Jones",
        "StartTime": "01:49:00.0000000",
        "FinishTime": "16:49:00.0000000",
        "BreakTime": "60",
        "TotalTime": 14,
        "Comments": "Test",
        "Rate": "10"
    }
]

I have the above array and I want to sum the TotalTime where the EmployeeName is the same. It should return a new array with like entries combined and the TotalTime added. I've used the below but it only returns two values because of Map. Is there a way I can achieve this while still maintaining all the values in the original array?

const CombinedArray = Array.from(Employees.reduce(
                        (m, {EmployeeName, TotalTime}) => m.set(EmployeeName, (m.get(EmployeeName) || 0) + TotalTime,), new Map
                        ), ([EmployeeName, TotalTime]) => ({EmployeeName, TotalTime}));
2
  • What is the desired output structure? Some of the properties in the array are different for the same employee across different objects. Would you want to just pick a random object, or the first, or what? Commented Jun 16, 2021 at 2:14
  • Apologies on that! I just realised, it's all test data which is why they're different. I want to pick a random object so if it's the first it doesn't matter. Commented Jun 16, 2021 at 2:40

1 Answer 1

2

In the Map, set the value not to the cumulative total time for the employee so far, but to a whole employee object that contains the total time inside it. Spread the first object found so as not to mutate the input.

var Employees=[{id:"382740",PayrollID:"8117817425",EmployeeName:"Bob Jones",StartTime:"15:15:00.0000000",FinishTime:"18:15:00.0000000",BreakTime:"45",TotalTime:2,Comments:"Test",Rate:"19"},{id:"439617",PayrollID:"8117817425",EmployeeName:"Peter Pan",StartTime:"16:15:00.0000000",FinishTime:"21:15:00.0000000",BreakTime:"60",TotalTime:4,Comments:"Test",Rate:"32"},{id:"201636",PayrollID:"5042289623",EmployeeName:"Bob Jones",StartTime:"09:56:00.0000000",FinishTime:"11:56:00.0000000",BreakTime:"45",TotalTime:1.25,Comments:"Test Comments",Rate:"19"},{id:"799653",PayrollID:"5042289623",EmployeeName:"Clarke Kent",StartTime:"16:49:00.0000000",FinishTime:"21:49:00.0000000",BreakTime:"60",TotalTime:4,Comments:"Test",Rate:"19"},{id:"951567",PayrollID:"5042289623",EmployeeName:"Bob Jones",StartTime:"01:49:00.0000000",FinishTime:"16:49:00.0000000",BreakTime:"60",TotalTime:14,Comments:"Test",Rate:"10"}];

const m = new Map();
for (const emp of Employees) {
  if (!m.has(emp.EmployeeName)) m.set(emp.EmployeeName, { ...emp });
  else m.get(emp.EmployeeName).TotalTime += emp.TotalTime;
}
console.log([...m.values()]);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.