I need a help for this example. Well, as you can see on picture, I have one array with so many objects. Some of this objects have same names. For example, we have name Abhay Singh 166 times. I need to find a solution, how I can find a same objects by name and sum working hours(hours between StarTimeUtc and EndTimeUtc).

Btw, I calculated hours between StarTimeUtc and EndTimeUtc and here is the code:
const a = new Date(startDate);
const b = new Date(endDate);
let difference = Math.abs(b.getTime() / 1000 - a.getTime() / 1000);
return Math.round(difference / 60 / 60);
}
So my table now looks like this:

As you can see, I have multiple same names and I need to sum total time in month for every user.
Thank you.
In pure Javascript/Typescript it would look like this:
interface Entry {
EmployeeName: string;
StartTimeUtc: string;
EndTimeUtc: string;
}
function calculateHours(entry: Entry): number {
const a = new Date(entry.StartTimeUtc);
const b = new Date(entry.EndTimeUtc);
let difference = Math.abs(b.getTime() / 1000 - a.getTime() / 1000);
return Math.round(difference / 60 / 60);
}
function usersHours(entries: Array<Entry>): Record<string, number> {
const hoursMap: Record<string, number> = {};
entries.forEach(entry => {
const hours = hoursMap[entry.EmployeeName] ?? 0;
hoursMap[entry.EmployeeName] = hours + calculateHours(entry);
});
return hoursMap;
}
If you are using some utility library like lodash, it can be rewritten to:
import { groupBy, sum, mapValues } from 'lodash';
function usersHours(entries: Array<Entry>) {
return mapValues(
groupBy(entries, e => e.EmployeeName),
values => sum(values.map(v => calculateHours(v)))
);
}