从json数据中获取父子关系中所有子节点的总和

Posted

技术标签:

【中文标题】从json数据中获取父子关系中所有子节点的总和【英文标题】:Get sum of All Children in parent and child relation from json data 【发布时间】:2021-12-26 07:28:27 【问题描述】:

我有一个 Json 数据,它有父、子、孙等关系,就像一棵树 他们每个人都有OpeningDebit和OpeningCredit。

我想获取每个孩子和大孩子的所有借方和贷方的总和,并将其添加到 parentID 为 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,
    ,
  ] ,

我试过了,但它说超出了最大调用堆栈大小

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 )

【问题讨论】:

向我们展示您的尝试,我们将尝试调试它 parent_id === obj.idparent_idid 都不存在于您的数据中 感谢您提及我已修复它,但我仍然没有得到任何东西 #ProGu 谢谢它解决了问题请在回答中写下你的评论我会投票非常感谢 【参考方案1】:
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
;

【讨论】:

以上是关于从json数据中获取父子关系中所有子节点的总和的主要内容,如果未能解决你的问题,请参考以下文章

一种定义父子节点关系的方法

js 根据Json数据的父子关系动态生成table 如图

JS根据子节点递归获取所有父节点的集合

读取省市区json文件,插入数据库表,表中保存父子节点对应关系

node节点是什么,有哪些神操作

沫沫金Sql查询树结构所有终极子节点