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

196
Visualizações
Cómo agregar días hábiles faltantes en un archivo csv

Necesito completar los días hábiles faltantes (es decir, cualquier día excepto el sábado y el domingo) en una serie. Así que digamos que tengo esta matriz:

 const arr = [ "Date,Value", "2020-12-31,1", "2021-01-01,-3", "2021-01-04,-38", "2021-01-05,-14", "2021-01-07,27", "2021-01-08,55", ]

"2021-01-06" es miércoles, pero falta en la matriz. Lo que debo hacer entonces es tomar el valor del día anterior (-14) y usarlo para "2021-01-06". Al final quedaría así:

 [ "Date,Value", "2020-12-31,1", "2021-01-01,-3", "2021-01-04,-38", "2021-01-05,-14", "2021-01-06,-14", "2021-01-07,27", "2021-01-08,55", ]

Actualmente estoy usando este algoritmo (que parece funcionar), pero me pregunto si se puede hacer de manera más eficiente:

 const fixEmptyBusinessDays = (arr) => { const newArr = []; if(arr.length){ newArr.push(arr[0]); arr.shift(); let latestValue = 0; for(let i = 0;i<arr.length;i++){ const el = arr[i]; if(el && el.length){ newArr.push(el); if(el.length > 1){ const val = el[1]; latestValue = val; } const currDate = el[0]; const currDateAsDateObject = new Date(currDate); if(currDate && currDateAsDateObject){ const day = currDateAsDateObject.getDay(); if((i+1)<arr.length && arr[i+1] && arr[i+1].length){ const next = arr[i+1]; const nextDayInFile = next[0]; const nextWeekDayUnformatted = get_next_weekday(new Date(currDate)); const nextMonth = (nextWeekDayUnformatted.getMonth()+1) < 10 ? `0${(nextWeekDayUnformatted.getMonth()+1)}` : (nextWeekDayUnformatted.getMonth()+1); const nextDay = nextWeekDayUnformatted.getDate() < 10 ? `0${nextWeekDayUnformatted.getDate()}` : nextWeekDayUnformatted.getDate(); const nextWeekDay = `${nextWeekDayUnformatted.getFullYear()}-${nextMonth}-${nextDay}` if(nextDayInFile !== nextWeekDay){ newArr.push([nextWeekDay, latestValue]); } } } } } } return newArr; } const arr = [ "Date,Value", "2020-12-31,1", "2021-01-01,-3", "2021-01-04,-38", "2021-01-05,-14", "2021-01-07,27", "2021-01-08,55", ] const result = fixEmptyBusinessDays(arr);
about 4 years ago · Juan Pablo Isaza
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