I need help with converting some array to desired format in JavaScript but I couldn't know how to handle it.
Basically I need to group my data by some of the fields in object. Could you give me some advise?
Here is my data:
[
{
"id": 52,
"subId": 42,
"title": "test title",
"languageId": 27,
"source": "test template lang 27",
"input": "2",
"output": "2",
},
{
"id": 52,
"subId": 42,
"title": "test title",
"languageId": 27,
"source": "test template lang 27",
"input": "1",
"output": "1",
},
{
"id": 52,
"subId": 42,
"title": "test title",
"languageId": 27,
"source": "test template lang 27",
"input": "3",
"output": "3",
},
{
"id": 52,
"subId": 42,
"title": "test title",
"languageId": 29,
"source": "test template of lang 29",
"input": "2",
"output": "2",
},
{
"id": 52,
"subId": 42,
"title": "test title",
"languageId": 29,
"source": "test template of lang 29",
"input": "1",
"output": "1",
},
{
"id": 52,
"subId": 42,
"title": "test title",
"languageId": 29,
"source": "test template of lang 29",
"input": "3",
"output": "3",
},
{
"id": 52,
"subId": 53,
"title": "test title 2",
"languageId": 22,
"source": "test template lang 22",
"input": "2",
"output": "2",
},
{
"id": 52,
"subId": 53,
"title": "test title 2",
"languageId": 22,
"source": "test template lang 22",
"input": "3",
"output": "3",
},
{
"id": 52,
"subId": 53,
"title": "test title 2",
"languageId": 16,
"source": "test template lang 16",
"input": "2",
"output": "2",
},
{
"id": 52,
"subId": 53,
"title": "test title 2",
"languageId": 16,
"source": "test template lang 16",
"input": "3",
"output": "3",
}
]
and I want to output:
[
{
"id": 52,
"subId": 42,
"title": "test title",
"lang": [
{"languageId": 27, "source": "test template lang 27"},
{"languageId": 29, "source": "test template of lang 29"}
],
"testCases": [
{"input": "2","output": "2"},
{"input": "1","output": "1"},
{"input": "3","output": "3"}
]
},
{
"id": 52,
"subId": 53,
"title": "test title",
"lang": [
{"languageId": 22, "source": "test template lang 22"},
{"languageId": 16, "source": "test template of lang 16"}
],
"testCases": [
{"input": "2","output": "2"},
{"input": "2","output": "3"}
]
}
]
In short I need to group this data by both lang id and testcase. Could you please help me to solve this problem?
Thank you.