Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Find same objects in array by name and sum working hours Angular

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). Response

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: table

As you can see, I have multiple same names and I need to sum total time in month for every user.

Thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)))
  );
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!