Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

74
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda