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

162
Views
Encuentre los mismos objetos en una matriz por nombre y sume las horas de trabajo Angular

Necesito ayuda para este ejemplo. Bueno, como puede ver en la imagen, tengo una matriz con tantos objetos. Algunos de estos objetos tienen los mismos nombres. Por ejemplo, tenemos el nombre Abhay Singh 166 veces. Necesito encontrar una solución, cómo puedo encontrar los mismos objetos por nombre y sumar las horas de trabajo (horas entre StarTimeUtc y EndTimeUtc). Respuesta

Por cierto, calculé las horas entre StarTimeUtc y EndTimeUtc y aquí está el código:

 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); }

Entonces mi tabla ahora se ve así: mesa

Como puede ver, tengo varios nombres iguales y necesito sumar el tiempo total en el mes para cada usuario.

Gracias.

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

0

En Javascript/Typescript puro se vería así:

 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; }

Si está utilizando alguna biblioteca de utilidades como lodash , se puede reescribir en:

 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!