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

107
Views
Combine valores duplicados por fecha en una matriz de JavaScript

Buenas tardes, necesito ayuda para combinar los datos de este array de ejemplo tomando la posición [0] de la fecha y la posición [1] del nombre (AA, BB, CC...)

 //Origin Array [ ['10/08/2022', 'AA', '08:00', '12:00', '', '17:00'], ['10/08/2022', 'BB', '08:01', '13:15', '14:16', '17:01'], ['10/08/2022', 'AA', '' , '' , '13:00', '' ], ['10/08/2022', 'CC', '09:00', '11:30', '12:30', '18:00'] ] //Result Array [ ['10/08/2022', 'AA', '08:00', '12:00', '13:00', '17:00'], ['10/08/2022', 'BB', '08:01', '13:15', '14:16', '17:01'], ['10/08/2022', 'CC', '09:00', '11:30', '12:30', '18:00'] ]

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

0

Te daré una respuesta de fuerza bruta. Como otros han señalado, existen funciones para mejorarlo.

Primero comprobaré si el par fecha/nombre ya existe. Si no, simplemente agregue los datos. Si lo hizo, actualice los espacios en blanco.

 originalArray = <your data> resultArray = [] function findDateNamePair(resultArray, aDate, aName) { // return index if resultArray[index][0] = aDate and resultArray[index][1] = aName // return -1 if not found for (var i=0; i < resultArray.length; i++) { if (resultArray[i][0] == aDate && resultArray[i][1] == aName) { return i; } } return -1; } for (var i=0; i < originalArray.length; i++) { const thisEntry = originalArray[i]; const existingIndex = findDateNamePair(resultArray, thisEntry[0], thisEntry[1]); if (existingIndex == -1) { resultArray.push(thisEntry); } else { for (var j=2; j<=5; j++) { if (resultArray[existingIndex][j] == '') { resultArray[existingIndex][j] = thisEntry[j]; } } } }
about 4 years ago · Juan Pablo Isaza Report

0

Puede usar el método de reduce para agrupar los elementos, y checkValue combinará solo los valores completos y Object.values para devolverlo a una matriz de disminución múltiple después de la reducción del objeto.

 const data = [ ['10/08/2022', 'AA', '08:00', '12:00', '', '17:00'], ['10/08/2022', 'BB', '08:01', '13:15', '14:16', '17:01'], ['10/08/2022', 'AA', '' , '' , '13:00', '' ], ['10/08/2022', 'CC', '09:00', '11:30', '12:30', '18:00'] ] const checkValue = (oldItem, newItem) => oldItem ? oldItem.map((item, i) => item || newItem[i]) : newItem const result = Object.values(data.reduce((acc, item) => ({ ...acc, [item[1]]: checkValue(acc[item[1]], item) }), {})) console.log(result)

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!