When I receive an array that looks like this:
errors = [
{
"row": 1,
"key": "volume",
"errorType": "Data type",
"expectedType": "number",
"receivedType": "string"
},
{
"row": 1,
"key": "units",
"errorType": "Required data",
"expectedType": "string"
},
{
"row": 3,
"key": "year",
"errorType": "Incorrect data type",
"expectedType": "number",
"receivedType": "string"
},
{
"row": 3,
"key": "make",
"errorType": "Required data",
"expectedType": "string"
}
]
My goal is to transform it into an array of objects structured like this:
const errorGrouped = [
{
row:1,
data:[
{
"key":"volume",
"errorType": "Data type",
"expectedType": "number",
"receivedType": "string"
},
{
"key": "units",
"errorType": "Required data",
"expectedType": "string"
}
]
},
{
row:3,
data:[
{
"key": "year",
"errorType": "Incorrect data type",
"expectedType": "number",
"receivedType": "string"
},
{
"key": "make",
"errorType": "Required data",
"expectedType": "string"
}
]
}
]
I have tried mapping through each object and destructuring them into the desired format of [{row:.., data:[...]}], but I'm struggling to group them efficiently. I believe there might be a better solution for this. Any help would be greatly appreciated. Thank you.