How to get/calculate the sum of child nodes for their parent node through jQuery or javascript

Viewed 325

Suppose I have a data in json format like this. this is hierarchical data with parent child relation.

{
  "data": [
    {
      "ID": "1",
      "AccountName": "a",
      "ParentID": "",
      "OpeningDebit": 0,
      "OpeningCredit": 0
    },
    {
      "ID": "2",
      "AccountName": "b",
      "ParentID": "1",
      "OpeningDebit": 0,
      "OpeningCredit": 0
    },
    {
      "ID": "3",
      "AccountName": "c",
      "ParentID": "2",
      "OpeningDebit": 100,
      "OpeningCredit": 200
    },
    {
      "ID": "4",
      "AccountName": "d",
      "ParentID": "2",
      "OpeningDebit": 50,
      "OpeningCredit": 10
    }
  ]
}

Parent child relationship maintained by ID and ParentID properties . Like the record with ID 2 is child record of the record with ID 1.

I want to sum of OpeningDebit and OpeningCredit property of all there children node and set it to their parent node. Remember only node that Transactional (Mentioned in their Property Type), has values.

My desire result is

{
  "data": [
    {
      "ID": "1",
      "AccountName": "a",
      "ParentID": "",
      "OpeningDebit": 150,
      "OpeningCredit": 210
    },
    {
      "ID": "2",
      "AccountName": "b",
      "ParentID": "1",
      "OpeningDebit": 150,
      "OpeningCredit": 210
    },
    {
      "ID": "3",
      "AccountName": "c",
      "ParentID": "2",
      "OpeningDebit": 100,
      "OpeningCredit": 200
    },
    {
      "ID": "4",
      "AccountName": "d",
      "ParentID": "2",
      "OpeningDebit": 50,
      "OpeningCredit": 10
    }
  ]
}

I want javascript function that can sum for all my Parent Node. may be it can be achieve through recursive function because this is nth level hierarchal data , like some nodes has 3 level and some has 5 .

1 Answers

Make a recursive function that calculate the sum of OpeningDebit and OpeningCredit, with the base case of when the data has no children, return its own value

Below snippet could help you

const { data } = {
  data: [
    {
      ID: "1",
      AccountName: "a",
      ParentID: "",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
    {
      ID: "2",
      AccountName: "b",
      ParentID: "1",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
    {
      ID: "3",
      AccountName: "c",
      ParentID: "2",
      OpeningDebit: 100,
      OpeningCredit: 200,
    },
    {
      ID: "4",
      AccountName: "d",
      ParentID: "2",
      OpeningDebit: 50,
      OpeningCredit: 10,
    },
  ],
}

const res = data.map((d) => {
  const getSum = (obj, prop) => {
    const children = data.filter(({ ParentID }) => ParentID === obj.ID)
    if (children.length === 0) return obj[prop]
    return children.reduce((acc, c) => acc + getSum(c, prop), 0)
  }

  return {
    ...d,
    OpeningDebit: getSum(d, "OpeningDebit"),
    OpeningCredit: getSum(d, "OpeningCredit"),
  }
})

console.log({ data: res })

Related