Upon receiving a response as shown below:
[
{id:1,name:"type1-something"},
{id:2,name:"something-type2"},
{id:3,name:"type3-something"},
{id:4,name:"something-type1"}
]
I have an Enum with all the names from the response stored in it:
enum E{
E1 = 'type1-something',
E2 = 'something-type2',
E3 = 'type3-something',
E4 = 'something-type1',
}
The task at hand is to group the response based on their names. For instance, the transformed output should look like this:
{
"Type1" : [{id:1,name:"type1-something"},{id:4,name:"something-type1"}],
"Type2" : [{id:2,name:"something-type2"}],
"Type3" : [{id:3,name:"type3-something"}],
}
What would be the best approach for this? One idea could be using a map
along with a for loop:
if (object.name == E1 || object.name == E4)
MAP['Type1'].push(object)
However, with over 30 entries in the enum, this solution might become convoluted and hard to manage. While incorporating smaller enums could reduce the code complexity, I am curious if there exists a simpler solution that I may not have considered.