Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!