I'm new to Lodash and trying to solve this problem but could find a good way to do it.
I have an array of objects return back from the database. The data is structured as below:
var data = [{
index: 1,
Aoo: "a1",
Boo: 'b2',
}, {
index: 1,
Aoo: "a2",
Boo: 'b2',
}, {
index: 2,
Aoo: "a3",
Boo: 'b3',
}];
I want to first group the objects by index, then group the attribute "Aoo" and "Boo" and put it in an array attribute called param
.
var result = [{
index: 1,
param: [{
Aoo: 'A1',
Boo: 'B1'
},{
Aoo: 'A2',
Boo: 'B2',
}]
}, {
index: 2,
param: [{
Aoo: 'A3',
Boo: 'B3'
}]
}
]
I can do it manually but I want to make the most use of Lodash's functionality. Right now I only know I can achieve the first step of grouping by using _.groupBy('index')
and I am stuck at what to do next.