I have this sample data that is returned from an API that I am trying to reform into an array with a sub-array of grouped objects. The raw data from the API looks like this:
[
{
"InsightId": 314,
"Classification": "Advantage",
"AttributeId": 14958,
"InsightAttribute": "Implementation speed",
"AttributeType": "Product Criterion"
},
{
"InsightId": 314,
"Classification": "Advantage",
"AttributeId": 14976,
"InsightAttribute": "Understanding your needs",
"AttributeType": "Sales Criterion"
},
{
"InsightId": 315,
"Classification": "Disadvantage",
"AttributeId": 17691,
"InsightAttribute": "Poor alignment with needs",
"AttributeType": "Product Criterion"
}
]
I want to group by the InsightId and create an object out of three of the properties: AttributeId, InsightAttribute, AttributeType.
And have the final array take the form of the following:
[
{
"InsightId": 314,
"Classification": "Advantage",
"Attributes" : [
{
"AttributeId": 14958,
"InsightAttribute": "Implementation speed",
"AttributeType": "Product Criterion"
},
{
AttributeId": 14976,
"InsightAttribute": "Understanding your needs",
"AttributeType": "Sales Criterion"
}
]
},
{
"InsightId": 315,
"Classification": "Disadvantage",
"Attributes" : [
{
"AttributeId": 17691,
"InsightAttribute": "Poor alignment with needs",
"AttributeType": "Product Criterion"
}
]
}
]
I'm new to Lodash and I've already spent hours going down a few rabbit holes that haven't worked out. I've realized it's time to turn to the experts. Any advice on how to get to the final result I've showed above?
Thanks for your help!