Home > Mobile >  Get sum of children in json object
Get sum of children in json object

Time:02-10

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

So at the end the parent should display 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": 328,
                            "debit": null,
                            "credit": "64.00",
                            "first_level_id": 46
                        },
                        {
                            "id": 330,
                            "debit": null,
                            "credit": "765.00",
                            "first_level_id": 46
                        },
                        {
                            "id": 338,
                            "debit": null,
                            "credit": "64.00",
                            "first_level_id": 46
                        },
                        {
                            "id": 340,
                            "debit": null,
                            "credit": "765.00",
                            "first_level_id": 46
                        },
                        {
                            "id": 462,
                            "debit": "64.00",
                            "credit": null,
                            "first_level_id": 46
                        },
                        {
                            "id": 464,
                            "debit": "765.00",
                            "credit": null,
                            "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": 314,
                            "debit": null,
                            "credit": "380.00",
                            "first_level_id": 47
                        },
                        {
                            "id": 324,
                            "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 >${parent.Nominal}</td>
                                <td >${parent.name}</td>
                                <td ></td>
                                <td >${sumOfChildrens(parent,parent.id)}</td>
                            </tr>
                        `);
                    });
                },
                error: function(data) {
                    alert(data['message']);
                    console.log(data['message']);
                },
            });
        });

CodePudding user response:

Stackoverflow isn't meant for other people to write code for you, but rather to answer specific questions or help solve problems. Instead of showing more code, explain what you've tried, which behavior you saw vs the one you expected. That being said, I think the following code snippet might help you clarify your question or even solve the problem on your own.


// for each first level child
data.children.map((item) => {
  // for each next level child
  item.children.map((item2) => {
    // this is the list of transactions
    console.log(item2.transactions);
    let sum = 0;
    // go over each transaction
    item2.transactions.map((transaction) => {
      // debit != null
      if (transaction.debit) {
        sum  = parseFloat(transaction.debit);
      }
    });
    // here you could add the sum to the parent
    console.log(sum);
  });
});

Try to edit this code so it works for you and come back if you have more questions.

  • Related