Home > Net >  Typescript - How to group a nested array
Typescript - How to group a nested array

Time:09-29

I'm having a hard time figuring this our because of the structure of the array. How can I group this array by quiz_title in typescript and assuming that I'm using an api.

Here is the structure of the array:

"UserScore": [
        {
            "id": 1,
            "user_id": "1",
            "quiz_id": "6",
            "module_id": "9",
            "remarks": "0",
            "users": {
                "id": 1,
                "name": "Carl",
                "email": "[email protected]"
            },
            "quiz_information": {
                "id": 6,
                "quiz_title": "Driving",
                "difficulty": "Moderate",
                "total_points": "15",
                "created_by": "7",
                "timer": "00:20:00",
                "module_id": "9",
                "passing_grade": null
            }
        },
        {
            "id": 2,
            "user_id": "4",
            "quiz_id": "6",
            "module_id": "9",
            "remarks": "0",
            "users": {
                "id": 4,
                "name": "GALLARDO",
                "email": "[email protected]",
            },
            "quiz_information": {
                "id": 6,
                "quiz_title": "Defensive",
                "difficulty": "Moderate",
                "total_points": "15",
                "created_by": "7",
                "updated_by": null,
                "timer": "00:20:00",
                "module_id": "9",
                "passing_grade": null
            }
        }

CodePudding user response:

I think this should do the work:

const UserScore = [
    {
        "id": 1,
        "user_id": "1",
        "quiz_id": "6",
        "module_id": "9",
        "remarks": "0",
        "users": {
            "id": 1,
            "name": "Carl",
            "email": "[email protected]"
        },
        "quiz_information": {
            "id": 6,
            "quiz_title": "Driving",
            "difficulty": "Moderate",
            "total_points": "15",
            "created_by": "7",
            "timer": "00:20:00",
            "module_id": "9",
            "passing_grade": null
        }
    },
    {
        "id": 2,
        "user_id": "4",
        "quiz_id": "6",
        "module_id": "9",
        "remarks": "0",
        "users": {
            "id": 4,
            "name": "GALLARDO",
            "email": "[email protected]",
        },
        "quiz_information": {
            "id": 6,
            "quiz_title": "Defensive",
            "difficulty": "Moderate",
            "total_points": "15",
            "created_by": "7",
            "updated_by": null,
            "timer": "00:20:00",
            "module_id": "9",
            "passing_grade": null
        }
    }
]

const grouped = UserScore.reduce((res, curr) => {
    return {...res, [curr.quiz_information.quiz_title]: [...(res[curr.quiz_information.quiz_title] || []), curr]}
}, {} as { [key: string]: any[] })


console.log(grouped)
  • Related