I need to re-organize an array of objects (from a database) into different groups according to one of the attributes of objects. To make it clearer, let's take the following example:
The ORM (Sequelize) returns the following array object:
[
{
id: 182,
employeeId: 'a',
skillId: 207,
score: 3,
},
{
id: 512,
employeeId: 'a',
skillId: 212,
score: 4,
},
{
id: 908,
employeeId: 'b',
skillId: 134,
score: 2,
},
{
id: 876,
employeeId: 'c',
skillId: 212,
score: 3,
},
]
I need to re-categorize the object grouping the entries by employeeId. Like this:
[
{
employeeId: 'a',
skills:
[
{ skillId: 203, score: 3},
{ skillId: 212, score: 4}
]
},
{
employeeId: 'b',
skills:
[
{ skillId: 134, score: 2}
]
},
{
employeeId: 'c',
skills:
[
{ skillId: 212, score: 3}
]
}
]
It didn't look hard at first but I'm hitting a roadblock, I already tried using reduce, forEarch and map but I only got to make an array of employeeIds, can't really get to include the skills array in each object of the array.
Any help would be appreciated.