Tomorrow is another day
function getTreeCount(treeArr) { let count = 0; for (let i = 0; i < treeArr.length; i++) { count++; if (treeArr[i].children) { count += getTreeCount(treeArr[i].children); } } return count;}// 输入let tree = [ { id: 1, children: [ { id: 2, children: [ { id: 3 } ] } ] }, { id: 4, children: [ { id: 5 } ] }];// 输出5console.log(getTreeCount(tree));
本文标题:统计数组对象的元素总数
文章作者:Canace
发布时间:2024-03-05
最后更新:2024-03-05
原始链接:https://canace.site/%E7%BB%9F%E8%AE%A1%E6%95%B0%E7%BB%84%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%85%83%E7%B4%A0%E6%80%BB%E6%95%B0/
版权声明:转载请注明出处