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

75
Visualizações
Chunk array based on conditional logic using ramda

I have an array of sizes that I’d like to reduce + chunk based on custom logic using ramda:

const sizes = [
  { size: 30 },
  { size: 10 },
  { size: 40 },
  { size: 20 },
  { size: 20 },
];

If the cumulative total exceeds the 50 threshold I would like to chunk them up, so that the result becomes:

const threshold = 50;

// 30 + 10 <= 50
// 40 <= 50
// 20 + 20 <= 50

const result = [40, 40, 40];

I’ve tried using reduceWhile and splitWhen but with no luck so far

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

0

Reduce the array to a new array. On each iterate, check if the current number + last item is greater than the max. If it is, set the current number as the last item. If not add the current number to the last item:

const sumMax = (max, arr) => arr.reduce((acc, n) =>
  (acc.at(-1) ?? 0) + n > max 
    ? [...acc, n]
    : [...acc.slice(0, -1), (acc.at(-1) ?? 0) + n]
  , []
)
    
const sizes = [{ size: 30 },{ size: 10 },{ size: 40 },{ size: 20 },{ size: 20 }];

const result = sumMax(50, sizes.map(o => o.size))

console.log(result)

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