I have a question, how to Convert Array to nested Object Array? I.e. I have a following array:
const myArray = [
{ "depart_id": 1, "depart_name": "computer science", "faculty_name": "faculty of natural science", "faculty_id": 1 },
{ "depart_id": 2, "depart_name": "computer programming", "faculty_name": "faculty of natural science", "faculty_id": 1 },
{ "depart_id": 3, "depart_name": "chemical engineering", "faculty_name": "faculty of engieering", "faculty_id": 2 },
{ "depart_id": 4, "depart_name": "marketing", "faculty_name": "faculty of business", "faculty_id": 3 },
]
And please explain me how I can convert this arrays to the following format:
const resultArray = [
{
"faculty_id": 1,
"faculty_name": "faculty of natural science",
"department": [
{
"depart_id": 1,
"depart_name": "computer science"
},
{
"depart_id": 2,
"depart_name": "computer programming"
}
]
},
{
"faculty_id": 2,
"faculty_name": "faculty of engieering",
"department": [
{
"depart_id": 3,
"depart_name": "chemical engineering"
}
]
},
{
"faculty_id": 3,
"faculty_name": "faculty of business",
"department": [
{
"depart_id": 4,
"depart_name": "marketing"
}
]
}
]