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

162
Vistas
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 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