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

128
Vistas
Nested Grouping of array of Objects based on properties

I have the data in a file in 5 columns - Location, Item Id, Inventory Number, Bin Number, Quantity.
Sample data-

>[
  {
    "Item ID": 12,
    "Location": "DEL",
    "Inventory Number": 18,
    "Bin Number": 3,
    "Quantity": 250
  },
  {
    "Item ID": 22,
    "Location": "DEL",
    "Inventory Number": 30,
    "Bin Number": 3,
    "Quantity": 50
  },
  {
    "Item ID": 12,
    "Location": "DEL",
    "Inventory Number": 19,
    "Bin Number": 3,
    "Quantity": 250
  },
  {
    "Item ID": 22,
    "Location": "DEL",
    "Inventory Number": 31,
    "Bin Number": 3,
    "Quantity": 50
  }
]

I want to group the data so that all the items for a location are grouped together at first, then all the items are grouped for the location and at last, the inventory number for unique items are grouped.

>{
    "DEL": [
        {
            "itemId": "12",
            "quantity": "500",
            "bins": [
                {
                    "inventorynumber": "18",
                    "binnumber": "3",
                    "quantity": "250"
                },
                {
                    "inventorynumber": "19",
                    "binnumber": "3",
                    "quantity": "250"
                }
            ]
        },
        {
            "itemId": "22",
            "quantity": "100",
            "bins": [
                {
                    "inventorynumber": "30",
                    "binnumber": "3",
                    "quantity": "50"
                },
                {
                    "inventorynumber": "31",
                    "binnumber": "3",
                    "quantity": "50"
                }
            ]
        }
    ]
}

I want to do it in pure JavaScript, I am able to group them separately.
Can anyone help me get the result in desired format?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You could find the inner group.

const
    data = [{ "Item ID": 12, Location: "DEL", "Inventory Number": 18, "Bin Number": 3, Quantity: 250 }, { "Item ID": 22, Location: "DEL", "Inventory Number": 30, "Bin Number": 3, Quantity: 50 }, { "Item ID": 12, Location: "DEL", "Inventory Number": 19, "Bin Number": 3, Quantity: 250 }, { "Item ID": 22, Location: "DEL", "Inventory Number": 31, "Bin Number": 3, Quantity: 50 }],
    result = data.reduce((r, { "Item ID": itemId, Location: location, "Inventory Number": inventorynumber, "Bin Number": binnumber, Quantity: quantity }) => {
        const temp = (r[location] ??= []).find(q => q.itemId === itemId);
        if (temp) {
            temp.quantity += quantity;
            temp.bins.push({ inventorynumber, binnumber, quantity });
        } else {
            r[location].push({ itemId, quantity, bins: [{ inventorynumber, binnumber, quantity }] });
        }
        return r;
    }, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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