Is there a way in ES6 or Ramda to group an array of objects by an object key and then create a new array of objects based on the grouping? For example, consider the following array of objects:
[
{
"name": "ABCD123",
"code": "GFDGF",
"ptj": {
"code": "123",
"desc": "ABCD 123"
}
},
...
]
I want to create a new array that's grouped by ptj.desc
or ptj.code
:
[
{
"PTJ": "ABCD 123",
"data": [
{
"name": "ABCD123",
"code": "GFDGF",
"ptj": {
"code": "123",
"desc": "ABCD 123"
}
},
...
]
},
...
]
I tried using the following code:
const stores = myArray.reduce((r, a) => {
r[a.ptj.desc] = r[a.ptj.desc] || [];
r[a.ptj.desc].push(a);
return r;
}, {});
However, this converts it into an object and I cannot use array.map
. Can you advise on how to correct and improve this using ES6 or Ramda? Thank you in advance.