Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda