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

198
Views
Cómo filtrar objetos ISO DateTime fuera del rango de intervalo de tiempo específico

Tengo una lista de objetos con propiedades startTime y endTime en formato ISO para un proyecto en el que estoy trabajando actualmente. Aquí hay una muestra:

 list = [ { startTime: '2022-06-26T10:00:00.000Z', endTime: '2022-06-26T10:30:00.000Z' }, { startTime: '2022-06-26T10:35:00.000Z', endTime: '2022-06-26T11:00:00.000Z' }, { startTime: '2022-06-26T11:45:00.000Z', endTime: '2022-06-26T12:15:00.000Z' }, { startTime: '2022-06-26T12:10:00.000Z', endTime: '2022-06-26T12:25:00.000Z' }, { startTime: '2022-06-26T12:20:00.000Z', endTime: '2022-06-26T12:45:00.000Z' }, { startTime: '2022-06-26T12:40:00.000Z', endTime: '2022-06-26T13:00:00.000Z' } ]

Y tengo una assignment de objeto que tiene dos propiedades como los objetos de la matriz de list anterior.

 const assignment = { startTime: '2022-06-26T12:00:00.000Z', endTime: '2022-06-26T12:30:00.000Z' }

Mi tarea es filtrar los objetos de la matriz de list que no caen en el intervalo/superposición con la assignment . Por ejemplo, quiero incluir lo siguiente en mi matriz de result porque no se superponen/dentro del intervalo de la assignment :

 const result = [ { startTime: '2022-06-26T10:00:00.000Z', endTime: '2022-06-26T10:30:00.000Z' }, { startTime: '2022-06-26T10:35:00.000Z', endTime: '2022-06-26T11:00:00.000Z' }, { startTime: '2022-06-26T12:40:00.000Z', endTime: '2022-06-26T13:00:00.000Z' } ]

Pero quiero excluir lo siguiente de mi matriz de result porque se superponen/dentro del intervalo del objeto de assignment .

 [ { startTime: '2022-06-26T11:45:00.000Z', endTime: '2022-06-26T12:15:00.000Z' }, { startTime: '2022-06-26T12:10:00.000Z', endTime: '2022-06-26T12:25:00.000Z' }, { startTime: '2022-06-26T12:20:00.000Z', endTime: '2022-06-26T12:45:00.000Z' }, ]

Esta es mi idea hasta ahora: Entonces, hay básicamente 5 escenarios.

 Scenario 1: startTime and endTime of a list object are before the startTime of assignment Scenario 2: startTime of a list object is before the startTime of the assignment but endTime is after startTime and before endTime of the assignment Scenario 3: startTime and endTime of a list object falls entirely into the interval of the startTime and endTime of the assignment object Scenario 4: startTime of a list object is after the startTime and before the endTime of assignment but endTime(list object) is after the endTime of the assignment Scenario 5: Both startTime and endTime of the list object are after the endTime of the assignment

Además de escribir muchas condiciones if-else , ¿cuál es el mejor enfoque para filtrar la lista de objetos? Estoy usando Javascript con la biblioteca Luxon. Cualquier ayuda sería muy apreciada.

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

0

Puede comprobar que los intervalos no se superponen comprobando que la hora de endTime de la list sea anterior a la startTime de inicio de la assignment o que la hora de startTime de la list sea posterior a la endTime de finalización de la assignment :

 list = [ { startTime: '2022-06-26T10:00:00.000Z', endTime: '2022-06-26T10:30:00.000Z' }, { startTime: '2022-06-26T10:35:00.000Z', endTime: '2022-06-26T11:00:00.000Z' }, { startTime: '2022-06-26T11:45:00.000Z', endTime: '2022-06-26T12:15:00.000Z' }, { startTime: '2022-06-26T12:10:00.000Z', endTime: '2022-06-26T12:25:00.000Z' }, { startTime: '2022-06-26T12:20:00.000Z', endTime: '2022-06-26T12:45:00.000Z' }, { startTime: '2022-06-26T12:40:00.000Z', endTime: '2022-06-26T13:00:00.000Z' } ] const assignment = { startTime: '2022-06-26T12:00:00.000Z', endTime: '2022-06-26T12:30:00.000Z' } const aDates = { startTime: new Date(assignment.startTime), endTime: new Date(assignment.endTime) } const result = list.filter(({ startTime, endTime }) => new Date(startTime) > aDates.endTime || new Date(endTime) < aDates.startTime ) console.log(result)
 .as-console-wrapper { max-height:100% !important; top: 0 }

Tenga en cuenta que he convertido las fechas en la assignment a Date fuera del bucle para guardar la conversión varias veces.

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!