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

244
Visualizações
How can I create a function which will take a date and return schedules based on time condition

This is my database structure:

[
    {
        "schedule_time": "2021-05-17 12:39:29",
        "slot": "L",
        "item_date": "2021-05-18"
    },
    {
        "schedule_time": "2021-05-17 12:47:53",
        "slot": "D",
        "item_date": "2021-05-18"
    },
    {
        "schedule_time": "2021-05-18 13:55:22",
        "slot": "D",
        "item_date": "2021-05-19"
    },
    {
        "schedule_time": "2021-05-19 16:09:15",
        "slot": "L",
        "item_date": "2021-05-20"
    },
    {
        "schedule_time": "2021-05-19 16:11:55",
        "slot": "L",
        "item_date": "2021-05-20"
    },

I want to create a function that will take item_date and it will show how many schedules are between 9am to 12am. 12am to 3pm, 3pm to 6pm, 6pm to 9pm on previous days. if you look at the database the item_date and schedule_date are not the same. That's why I want the previous day's schedules listed in an array.

I have created a function it's only returning 9am to 12am data. I want the schedules together in an array.

  function schedulesList (date){
        let dbItemDate = date;
        let otherDayArray = schedules.filter(num => num.item_date !== dbItemDate); // not equal because I want other dates schedules not the exact date
        let times = otherDayArray?.filter(num => num.schedule_time.split(' ')[1] >= '12:00:00' && num.schedule_time.split(' ')[1] <= '14:00:00');
        console.log('9am to 12pm', times.length, 'schedules') //
    }

The expected result should be similar to this:

[
'9am to 12pm' : 2,
'12pm to 3pm' : 0,
'3pm to 6pm' : 1,
'6pm to 9pm' : 3,
]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Assuming you've stored the given input array in a variable called data.

let data = [{
    "schedule_time": "2021-05-17 12:39:29",
    "slot": "L",
    "item_date": "2021-05-18"
  },
  {
    "schedule_time": "2021-05-17 12:47:53",
    "slot": "D",
    "item_date": "2021-05-18"
  },
  {
    "schedule_time": "2021-05-18 13:55:22",
    "slot": "D",
    "item_date": "2021-05-19"
  },
  {
    "schedule_time": "2021-05-19 16:09:15",
    "slot": "L",
    "item_date": "2021-05-21"
  },
  {
    "schedule_time": "2021-05-19 16:11:55",
    "slot": "L",
    "item_date": "2021-05-20"
  }
];

//function to get the final results
let getScheduleCounter = (date) => {
  let scheduleCounter = {
    '9_12': [],
    '12_3': [],
    '3_6': [],
    '6_9': []
  };

  data.forEach(sch => {
    if (sch.item_date !== date) {
      updateScheduledCounter(sch, scheduleCounter);
    }
  });

  return scheduleCounter;
}

//function to get the count and update in scheduleCounter
let updateScheduledCounter = (sch, scheduleCounter) => {
  let time = sch.schedule_time.split(' ')[1];
  switch (!!time) {
    case (time <= '12:00:00'):
      scheduleCounter['9_12']++;
      break;

    case (time >= '12:00:00' && time <= '15:00:00'):
      scheduleCounter['12_3']++;
      break;

    case (time >= '15:00:00' && time <= '18:00:00'):
      scheduleCounter['3_6']++;
      break;

    case (time >= '18:00:00' && time <= '21:00:00'):
      scheduleCounter['6_9']++;
      break;
  }
}

let schedules = getScheduleCounter("2021-05-20");
console.log('schedules: ', schedules);

about 4 years ago · Juan Pablo Isaza Relatório

0

I suggest the following approach to the solution

const schedules = [
{"schedule_time": "2021-05-17 12:39:29","slot": "L","item_date": "2021-05-18"},
{"schedule_time": "2021-05-17 12:47:53","slot": "D","item_date": "2021-05-18"},
{"schedule_time": "2021-05-18 13:55:22","slot": "D","item_date": "2021-05-19"},
{"schedule_time": "2021-05-19 16:09:15","slot": "L","item_date": "2021-05-20"},
{"schedule_time": "2021-05-19 16:11:55","slot": "L","item_date": "2021-05-20"}];

const hours = [
{hoursName: '9am to 12pm', from: 9, to: 12},
{hoursName: '12pm to 3pm', from: 12, to: 15},
{hoursName: '3pm to 6pm', from: 15, to: 18},
{hoursName: '6pm to 9pm', from: 18, to: 21}];

const targetDate = new Date("2021-05-20");

const previousItems = schedules.filter(({ schedule_time }) => {
  const scheduleTime = new Date(schedule_time);
  return scheduleTime < targetDate;
});

const result = previousItems.reduce((acc, { schedule_time }) => {
  const scheduleTime = new Date(schedule_time);
  const targetHour = scheduleTime.getHours();
  const { hoursName } = hours.find(({ from, to }) => (targetHour >= from) && (targetHour < to));
  acc[hoursName] = (acc[hoursName] ?? 0) + 1;
  return acc;
}, {});

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

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