0

I have a flat array consisting of the following data

[{
        "text": "",
        "type": "true_false",
        "ans": {
            "ans_1": true,
            "ans_2": false
        },
        "correct_ans": "",
        "reason": "",
        "passage": ""
    },
    {
        "text": "",
        "type": "true_false",
        "ans": {
            "ans_1": true,
            "ans_2": false
        },
        "correct_ans": "",
        "reason": "",
        "passage": ""
    }, {
        "text": "",
        "type": "passage",
        "ans": {
            "ans_1": "",
            "ans_2": ""
        },
        "correct_ans": [],
        "reason": "",
        "passage": 3
    }, {
        "text": "",
        "type": "multichoice",
        "ans": {
            "ans_1": "",
            "ans_2": ""
        },
        "correct_ans": [],
        "reason": "",
        "passage": ""
    }, {
        "text": "",
        "type": "passage",
        "ans": {
            "ans_1": "",
            "ans_2": ""
        },
        "correct_ans": [],
        "reason": "",
        "passage": 3
    }, {
        "text": "",
        "type": "passage",
        "ans": {
            "ans_1": "",
            "ans_2": ""
        },
        "correct_ans": [],
        "reason": "",
        "passage": 5
    }
 ]

But, I will like to group the array in the format below.

  [{
        "text": "",
        "type": "true_false",
        "ans": {
            "ans_1": true,
            "ans_2": false
        },
        "correct_ans": "",
        "reason": "",
        "passage": ""
    },
    {
        "text": "",
        "type": "true_false",
        "ans": {
            "ans_1": true,
            "ans_2": false
        },
        "correct_ans": "",
        "reason": "",
        "passage": ""
    }, {
        "type": "passage",
        "passage": 3,
        "children": [{
            "text": "",
            "ans": {
                "ans_1": "",
                "ans_2": ""
            },
            "correct_ans": [],
            "reason": ""
        }, {
            "text": "",
            "ans": {
                "ans_1": "",
                "ans_2": ""
            },
            "correct_ans": [],
            "reason": ""
        }]
    }, {
        "text": "",
        "type": "multichoice",
        "ans": {
            "ans_1": "",
            "ans_2": ""
        },
        "correct_ans": [],
        "reason": "",
        "passage": ""
    }, {
        "passage": 5,
        "type": "passage",
        "children": [{
            "text": "",
            "ans": {
                "ans_1": "",
                "ans_2": ""
            },
            "correct_ans": [],
            "reason": ""
        }]
    }
 ]

I have searched online but; couldn't get my desired result. I also cam across this Q/A Build tree array from flat array in javascript. I tried to tweak the answer but still couldn't get my desired result.

0 Answers0