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 .