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

155
Views
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
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!