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

138
Visualizações
how to get element based on datetime in js

i have an object like this:

0: {start: '2022-02-23T09:30:00', end: '2022-02-23T10:00:00',…}
    1: {start: '2022-02-23T10:00:00', end: '2022-02-23T10:30:00',…}
    2: {start: '2022-02-23T10:30:00', end: '2022-02-23T11:00:00',…}
    3: {start: '2022-02-23T11:00:00', end: '2022-02-23T11:30:00',…}
    4: {start: '2022-02-23T12:00:00', end: '2022-02-23T12:30:00',…}
    5: {start: '2022-02-23T13:00:00', end: '2022-02-23T13:30:00',…}
    6: {start: '2022-02-23T13:30:00', end: '2022-02-23T14:00:00',…}
    7: {start: '2022-02-23T14:00:00', end: '2022-02-23T14:30:00',…}
    8: {start: '2022-02-23T14:30:00', end: '2022-02-23T15:00:00',…}
    9: {start: '2022-02-23T15:00:00', end: '2022-02-23T15:30:00',…}
    10: {start: '2022-02-23T15:30:00', end: '2022-02-23T16:00:00',…}
    11: {start: '2022-02-23T16:00:00', end: '2022-02-23T16:30:00',…}
    12: {start: '2022-02-23T16:30:00', end: '2022-02-23T17:00:00',…}

what i'm trying to do is getting the first element that has a free time in between those start and end

For example, if we look at the third element it starts at 11:00 and ends at 11:30 and the previous elements are contiguos. The 4th element instead starts at 12:00 so from 11:30 and 12:00 there are 30 minutes free. So what i need to do is get the end (or the entire element) property of the first element that has a next element that doesn't have a contiguos start

update: i tried something like this

const latestAppointment = [...appointments].sort((a, b) => (new Date(a.end)) < (new Date(b.end)) ? -1 : 1)

i hope i explained myself Thank you

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Looking for something like this?

myArray.filter((e, i, o) => e.end !== o[i+1]?.start)?.shift();

const times = {
  0: {
    start: '2022-02-23T09:30:00',
    end: '2022-02-23T10:00:00'
  },
  1: {
    start: '2022-02-23T10:00:00',
    end: '2022-02-23T10:30:00'
  },
  2: {
    start: '2022-02-23T10:30:00',
    end: '2022-02-23T11:00:00'
  },
  3: {
    start: '2022-02-23T11:00:00',
    end: '2022-02-23T11:30:00'
  },
  4: {
    start: '2022-02-23T12:00:00',
    end: '2022-02-23T12:30:00'
  },
  5: {
    start: '2022-02-23T13:00:00',
    end: '2022-02-23T13:30:00'
  },
  6: {
    start: '2022-02-23T13:30:00',
    end: '2022-02-23T14:00:00'
  },
  7: {
    start: '2022-02-23T14:00:00',
    end: '2022-02-23T14:30:00'
  },
  8: {
    start: '2022-02-23T14:30:00',
    end: '2022-02-23T15:00:00'
  },
  9: {
    start: '2022-02-23T15:00:00',
    end: '2022-02-23T15:30:00'
  },
  10: {
    start: '2022-02-23T15:30:00',
    end: '2022-02-23T16:00:00'
  },
  11: {
    start: '2022-02-23T16:00:00',
    end: '2022-02-23T16:30:00'
  },
  12: {
    start: '2022-02-23T16:30:00',
    end: '2022-02-23T17:00:00'
  }
}


const res = Object.values(times).filter((e, i, o) => e.end !== o[i+1]?.start)?.shift();
console.log(res);

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