SOURCE

let chapter= {
  title: '第一章',
  linkCount: 1,
  children: [
      {
          title: '第一节',
          linkCount: 2,
          children: [
              {
                  title: '(1)',
                  linkCount: 4
              },
              {
                  title: '(2)',
                  linkCount: 6
              }
          ]
      },
      {
          title: '第二节',
          linkCount: 2,
          children: [
              {
                  title: '(3)',
                  linkCount: 9
              },
              {
                  title: '(4)',
                  linkCount: 8
              }
          ]
      }
  ]
}

let linkCount = 0
function getCount(chapter) {
    if(!chapter.children) {
        return chapter.linkCount
    } else {
        linkCount = chapter.linkCount
        for(let i=0; i<chapter.children.length; i++) {
            linkCount += getCount(chapter.children[i])
        }
        return linkCount
    }
}
console.log(getCount(chapter))


console 命令行工具 X clear

                    
>
console