'Get sum of children in json object

I have a JSON object which has multiple children and grandchildren I want to get the sum of grandchildren and show it with the children, similar get the sum of children and show it with the parent.

So in the end, the parent should display the sum of all children.

{
    "id": 4,
    "Nominal": "4",
    "name": "Revenue",
    "type": "Revenue",
    "parent_id": 0,
    "children": [
        {
            "id": 14,
            "Nominal": "41",
            "name": "Operational Revenue",
            "parent_id": 4,
            "transactions_sum_debit": null,
            "transactions_sum_credit": null,
            "children": [
                {
                    "id": 46,
                    "Nominal": "4101",
                    "name": "Revenue of Products and services Sales",
                    "parent_id": 41,
                    "transactions_sum_debit": "1658.00",
                    "transactions_sum_credit": "3316.00",
                    "children": [],
                    "transactions": [
                        {
                            "id": 308,
                            "debit": null,
                            "credit": "64.00",
                            "first_level_id": 46
                        },
                        {
                            "id": 310,
                            "debit": null,
                            "credit": "765.00"
                        },
                        {
                            "id": 318,
                            "debit": null,
                            "credit": "64.00",
                            "first_level_id": 46,
                            "invoice_id": 20
                        },
                        {
                            "id": 320,
                            "debit": null,
                            "credit": "765.00",
                            "first_level_id": 46
                        },
                        {
                            "id": 340,
                            "debit": null,
                            "credit": "765.00",
                            "first_level_id": 46
                        },
                        {
                            "id": 466,
                            "debit": "64.00",
                            "credit": null,
                            "first_level_id": 46
                        },
                        {
                            "id": 468,
                            "debit": "765.00",
                            "credit": null,
                            "first_level_id": 46
                        }
                    ]
                }
            ],
            "transactions": []
        },
        {
            "id": 15,
            "Nominal": "42",
            "parent_id": 4,
            "transactions_sum_debit": null,
            "transactions_sum_credit": null,
            "children": [
                {
                    "id": 47,
                    "Nominal": "4201",
                    "parent_id": 42,
                    "transactions_sum_debit": null,
                    "transactions_sum_credit": "1520.00",
                    "children": [],
                    "transactions": [
                        {
                            "id": 304,
                            "debit": null,
                            "credit": "380.00",
                            "first_level_id": 47
                        },
                        {
                            "id": 334,
                            "debit": null,
                            "credit": "380.00",
                            "first_level_id": 47
                        }
                    ]
                }
            ],
            "transactions": []
        }
    ]
}

for example in the above JSON data I want to get the sum transactions_sum_debit from all the children and show it one level up. until the parent_id = 0

My View:

        //get sum of all children in json
        function sumOfChildrens(parent, id) {
            let data = parent;
            let ids = id;
            let transactions_sum_credit = 0;
            //call the function recursively until the last child
            for (let i = 0; i < data.length; i++) {
                if (data[i].id == ids) {
                    transactions_sum_credit += parseFloat(data[i].transactions_sum_credit);
                }
                if (data[i].children) {
                    transactions_sum_credit += sumOfChildrens(data[i].children, ids);
                }
            }
            console.log(transactions_sum_credit);
            return transactions_sum_credit;
        }

        $(document).ready(function() {
            $.ajax({
                headers: {
                    "Authorization": 'Bearer ' + sessionStorage.getItem('token')
                },
                type: "get",
                url: "{{ URL::asset('api/reports/get-generalLedger') }}",
                success: function(data) {
                    var data = data.data;
                    console.log(data);
                    $("#tablebody").empty();
                    data.map(parent => {
                        $("#tablebody").append(
                            `
                            <tr>
                                <td class="${parent.Nominal} id_${parent.id}">${parent.Nominal}</td>
                                <td class="${parent.Nominal} id_${parent.id}">${parent.name}</td>
                                <td class="${parent.Nominal} id_${parent.id}"></td>
                                <td class="${parent.Nominal} id_${parent.id}">${sumOfChildrens(parent,parent.id)}</td>
                            </tr>
                        `);
                    });
                },
                error: function(data) {
                    alert(data['message']);
                    console.log(data['message']);
                },
            });
        });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source