Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda