Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda