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.