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

170
Visualizações
Recursive function to check length of an array and if its less than 10 in length, add from another array to the first array until I hit 10 in length

So what I am basically trying to do is I query for a list of available programs for a week period. I format that into an object for when selecting a single day, I can easily use object look up for that day, and create programsOnSelectedDay.

programDateLookUp {
  "22/04/24": [
    {
      "programType": "CLINIC",
      "start_date": "2022-04-24T16:00:00.000Z"
    }
  ],
  "22/04/25": [
    {
      "programType": "PRACTICE",
      "start_date": "2022-04-25T16:00:00.000Z"
    },
    {
      "programType": "PICKUP",
      "start_date": "2022-04-25T16:00:00.000Z"
    }
  ],
  "22/04/27": [
    {
      "programType": "PRACTICE",
      "start_date": "2022-04-27T16:00:00.000Z"
    }
  ],
  "22/04/28": [
    {
      "programType": "CLINIC",
      "start_date": "2022-04-28T16:00:00.000Z"
    }
  ]
}

I then grab the programsOnSelectedDay based on the Object keys like so

const programsOnSelectedDay = programDateLookUp[selectedDay] || [];
where selectedDay = '22/04/25'
 LOG  programsOnSelectedDay [
  {
    "programType": "PRACTICE",
    "start_date": "2022-04-25T16:00:00.000Z"
  },
  {
    "programType": "PICKUP",
    "start_date": "2022-04-25T16:00:00.000Z"
  }
]

Now I need a recursive function I think to be able to check the length of programsOnSelectedDay, see that it is less then 10, and add the next day programs to that programsOnSelectedDay array, and so on until I hit at least 10 results or I have gone through the remaining days in the week.

As it stands, I have something like this which will just hit maximum depth exceeded

  const addProgramsBasedOnLength = (programs) => {
    if (programs.length === 0) return programs;

    if (programs.length < 10) {
      const nextDay = moment(selectedDate, FORMAT).add(1, 'days').format(FORMAT);
      const nextDayPrograms = programDateLookUp[nextDay] || [];
      return addProgramsBasedOnLength([...programs, ...nextDayPrograms]);
    }
    return programs;
  };

If I only return [...programs,...nextDayPrograms] instead of the function calling that new array, it does work in giving me the next day results only. So I'm wondering how to make this recursive to give me up to 10 results added to the original programsOnSelectedDay OR until the rest of the week as been gone through.

Any know of a way to accomplish this?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I tried something very similar to @Ouroborus comment.
Instead looping through the week array that had formatted dates for the current selected week; using the index of whatever date I have already selected.
Below solution allows to only adding days of the week that is past the selectedDate and not looping over days that are before my selectedDate.

const addProgramsBasedOnLength = (programs, programDateLookUp, selectedDate, week) => {
  if (programs.length >= 10) return programs;

  const indexOfSelectedDate = week.findIndex(day => day.formatted === selectedDate);

  const restOfWeekPrograms = week.slice(indexOfSelectedDate + 1).reduce((acc, { formatted }) => [
    ...acc,
    ...programDateLookUp[formatted] || [],
  ], []);

  return [...programs, { key: 'noMoreResultsForDay' }, ...restOfWeekPrograms];
};

added the key object there for UI purposes when I map through the returned array

about 4 years ago · Santiago Trujillo 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