js树形结构父节点等于直属子节点相加 求答案?

父节点的分数值socre等于直属子节点的score之和,只有最有一级节点的score有值,所以需要倒序求出每个父节点score的值,没想出来要怎么搞,求助大神的帮助

总结就是:3级加起来等于2级,2级加起来等于1级

let treeData = [{
    level: "1",
    parentCode: "0",
    score: "",
    setItemCode: "cdx10031",
    setItemContent: "一级内容1",
    sortNumber: 1,
    children: [{
        level: "2",
        parentCode: "cdx10031",
        score: "",
        setItemCode: "cdx10061"
        setItemContent: "二级内容1-1"
        sortNumber: 1,
        children: [{
            level: "3",
            parentCode: "cdx10061",
            score: "1",
            setItemCode: "cdx10090"
            setItemContent: "三级级内容1-1-1"
            sortNumber: 1,
        },{    
            level: "3",
            parentCode: "cdx10061",
            score: "2",
            setItemCode: "cdx10091"
            setItemContent: "三级内容1-1-2"
            sortNumber: 2,
            }]
    },{    
        level: "2",
        parentCode: "cdx10031",
        score: "",
        setItemCode: "cdx10062"
        setItemContent: "二级内容1-2"
        sortNumber: 2,
        children: [{
            level: "3",
            parentCode: "cdx10062",
            score: "3",
            setItemCode: "cdx10092"
            setItemContent: "三级级内容1-2-1"
            sortNumber: 1,
        },{    
            level: "3",
            parentCode: "cdx10062",
            score: "4",
            setItemCode: "cdx10093"
            setItemContent: "三级内容1-2-2"
            sortNumber: 2,
            }]
    }]
},{
    level: "1",
    parentCode: "0",
    score: "",
    setItemCode: "cdx10035",
    setItemContent: "一级内容2",
    sortNumber: 2,
    children: [{
        level: "2",
        parentCode: "cdx10035",
        score: "",
        setItemCode: "cdx10065"
        setItemContent: "二级内容2-1"
        sortNumber: 1,
        children: [{
            level: "3",
            parentCode: "cdx10065",
            score: "1",
            setItemCode: "cdx10095"
            setItemContent: "三级级内容2-1-1"
            sortNumber: 1,
        },{    
            level: "3",
            parentCode: "cdx10065",
            score: "2",
            setItemCode: "cdx10096"
            setItemContent: "三级内容2-1-2"
            sortNumber: 2,
            }]
    },{    
        level: "2",
        parentCode: "cdx10035",
        score: "",
        setItemCode: "cdx10066"
        setItemContent: "二级内容2-2"
        sortNumber: 2,
        children: [{
            level: "3",
            parentCode: "cdx10066",
            score: "3",
            setItemCode: "cdx10097"
            setItemContent: "三级级内容2-2-1"
            sortNumber: 1,
        },{    
            level: "3",
            parentCode: "cdx10066",
            score: "4",
            setItemCode: "cdx10098"
            setItemContent: "三级内容2-2-2"
            sortNumber: 2,
            }]
    }]
}]
阅读 2.1k
3 个回答
✓ 已被采纳新手上路,请多包涵

先算出二级的score,之后再算一级的score

level2Score(countyTargetData, {}, '3')
level2Score(countyTargetData, {}, '2')
function level2Score(tableArr, row, level) {
  for (var i = 0; i < tableArr.length; i++) {
    if (tableArr[i].level === level) {
      row.score = Add(row.score, tableArr[i].score)
    }
    if (tableArr[i].children && tableArr[i].children.length > 0) {
          if (tableArr[i].level * 1 + 1 === level * 1) {
            tableArr[i].score = 0
          }
          level2Score(tableArr[i].children, tableArr[i], level)
        }
    }
}
function calculateScore(node) {
   
    if (!node.children || node.children.length === 0) {
        return parseInt(node.score, 10);
    }
    let totalScore = 0;
    for (let child of node.children) {
        totalScore += calculateScore(child);
    }
    node.score = totalScore;

    return totalScore;
}

treeData.forEach(node => {
    calculateScore(node);
});

console.log(treeData);
function updateTreeScore(tree) {
  tree.forEach(node => node.score = (node.children?.[0]?.score ? node.children : updateTreeScore(node.children)).reduce((sum, v) => sum + (+v.score||0), 0) )
  return tree
}
updateTreeScore(treeData)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏