简体   繁体   中英

Get sum of All Children in parent and child relation from json data

I have a Json Data that have a parent, child, grandchild and so on relation just like a tree each of them have OpeningDebit and OpeningCredit.

I want to get the sum of all Debits and Credits of Each children and grand Children and add it to the parent which have parentID of 0

在此处输入图片说明

const { data } = {
  data: [          {
      ID: '1',
      AccountName: "Assets",
      ParentID: "0",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '2',
      AccountName: "Liabilities",
      ParentID: "0",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '3',
      AccountName: "Revenue",
      ParentID: "0",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '4',
      AccountName: "Expenses",
      ParentID: "0",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '5',
      AccountName: "Eqity",
      ParentID: "0",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '39',
      AccountName: "aa",
      ParentID: "38",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '40',
      AccountName: "aaa",
      ParentID: "38",
      OpeningDebit: 60,
      OpeningCredit: 120,
    },
            {
      ID: '41',
      AccountName: "bb",
      ParentID: "39",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '42',
      AccountName: "bbb",
      ParentID: "39",
      OpeningDebit: 180,
      OpeningCredit: 240,
    },
            {
      ID: '6',
      AccountName: "Inventory",
      ParentID: "1",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '7',
      AccountName: "Banks",
      ParentID: "1",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '8',
      AccountName: "Accounts receivable",
      ParentID: "1",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '9',
      AccountName: "Current assets",
      ParentID: "1",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '10',
      AccountName: "Other Assets",
      ParentID: "1",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '11',
      AccountName: "Petty Cash",
      ParentID: "1",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '38',
      AccountName: "a",
      ParentID: "1",
      OpeningDebit: 0,
      OpeningCredit: 0,
    },
            {
      ID: '43',
      AccountName: "bbbb",
      ParentID: "41",
      OpeningDebit: 300,
      OpeningCredit: 360,
    },
  ] ,
}

I have tried this but it says Maximum call stack size exceeded

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,
    debit: getSum(d, "OpeningDebit"),
    credit: getSum(d, "OpeningCredit"),
  }
})

console.log({ data: res })

在此处输入图片说明

const concert = (arr, id = '0') => {
  const res = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].ParentID === id) {
      const obj = {...arr[i]};
      res.push(obj)
      obj.children =concert(arr,obj.ID)
    }
  }
  return res
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM