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

185
Vistas
Split array of objects into new array or objects based on given value

I have an array of objects that look something like this;

value = [{id:1, count:50},{id:1, count:25}]

I need below three senario as below. scenario :- 1

result1 = [
  [ { id: 1, qty: 25 } ],
  [ { id: 1, qty: 25 } ],
  [ { id: 2, qty: 25 } ]
]

scenario:- 2

  result2 = [ [ { id: 1, qty: 50 }, { id: 2, qty: 25 } ] ]

scenario:-3

[ [ { id: 1, qty: 50 }, { id: 2, qty: 15 } ], [ { id: 2, qty: 10 }] ]

I have resolved scenario 1 and scenario 2, however getting logic issues in scenario 3.

please see below JS code

let temp = 0;
        let arr = [];
        let result = [];
        let receive_list = [
            { id: 1, qty: 50 },
            { id: 2, qty: 25 },
        ]
        const packing_weight = 65;
            receive_list.forEach(item => {
            let availableQty = item.qty;//50
            while (availableQty > 0) {
                if (availableQty >  packing_weight) {
                    arr.push({ id: item.id, qty: packing_weight });
                    availableQty = availableQty - packing_weight; //25
                    temp += packing_weight;
                }
                else {
                    arr.push({ id: item.id, qty: availableQty });
                    temp += availableQty;
                    availableQty = 0;
                }
                if(temp === packing_weight){
                    result.push(arr);
                    temp = 0;
                    arr = [];
                }
            }
        })

I need below result which I am getting empty array result = [] instead of below result [ [ { id: 1, qty: 50 }, { id: 2, qty: 15 } ], [ { id: 2, qty: 10 }] ]

about 4 years ago · Juan Pablo Isaza
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