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

156
Visualizações
How to optimize reducer function

I have this function

const joinCages = arr => {
    return arr.reduce((acc, { jaulaId, comunaCode }) => {
        if (!acc.hasOwnProperty(jaulaId)) acc[jaulaId] = []
        acc[jaulaId].push(comunaCode)
        return acc
    }, {})
}

The output is this:

{
  JAU0000001: [ '130109' ],
  JAU0000029: [ '100102', '100103' ],
  JAU0000017: [ '100304', '100305' ]
}

What it does:

Create an object that group the "Comuna Code" in the key "jaula ID" from an array of objects

Data array

[
    {
        indexId: 104834,
        jaulaId: "JAU0000001",
        distributionCenterId: "100",
        comunaCode: "130109",
        orderType: "Retorno",
        regionCode: "13"
    },
    {
        indexId: 104836,
        jaulaId: "JAU0000029",
        distributionCenterId: "100",
        comunaCode: "100102",
        orderType: "Retorno",
        regionCode: "13"
    },
    {
        indexId: 104837,
        jaulaId: "JAU0000029",
        distributionCenterId: "100",
        comunaCode: "100103",
        orderType: "Retorno",
        regionCode: "13"
    }
]

I want to upgrade that function without the if statement and removing the return...Return implicit you know?

The problem is that I am failing coding this and I dont know where or how can I do this.

Upgraded function

const joinCages2 = arr => {
    return arr.reduce((acc, { jaulaId, comunaCode }) => ({
        ...acc,
        [jaulaId]: [jaulaId] ? [jaulaId].push(comunaCode) : [comunaCode]
    }), {})
}

Thank you in advance.

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