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

167
Views
How to loop through JSON and add values to object

So I am having trouble figuring out how to create an array with two objects, looping through my object and adding some values to those objects in Javascript. Currently I have the following mock response:

const mockResponse = 
        {
            "errors": [
              {
                "errorKey": "ERROR_NO_DELIVERY_OPTIONS",
                "errorParameters": [
                  {
                    "errorMessage": "ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW",
                    "partNumbers": [
                      19308033,
                      19114798
                    ]
                  },
                  {
                    "errorMessage": "Ship to Home not available for these orderItemId",
                    "orderItemIds": [
                      10315031,
                      10315032
                    ],
                    "availableShipModeId": 13203
                  },
                  {
                    "errorMessage": "Pickup At Seller not available for these orderItemIds",
                    "orderItemIds": [
                      10222222,
                      10333333
                    ],
                    "availableShipModeId": 13203
                  }
                ],
                "errorMessage": "ERROR_NO_DELIVERY_OPTIONS",
                "errorCode": "ERROR_NO_DELIVERY_OPTIONS"
              }
            ]
          }

I would like to have an array with two objects. One for the first error message("Ship to home...") and another for the second error message("Pickup at Seller..."). I would like to then loop through the JSON and add each "orderItemIds" to there respective object. For example, 10315031,10315032 would go to the first object and 10222222, 10333333 to the second.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use reduce to loop through your errors and use the errorMessage property as a key

const result = mockResponse.errors[0].errorParameters.reduce((prev, item) => {
    const { errorMessage, orderItemIds } = item;
    if (prev[errorMessage]) {
        prev[errorMessage] = [...prev[errorMessage], ...orderItemsIds];
    } else {
        prev[errorMessage] = orderItemIds
    }
    return prev
}, {})

Let me know if this does answer your question

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