0
     [
{
  "clockInState": 0,
  "sectionNum": "1",
  "courseBegin": "2019-02-09 00:00:00.0",
  "teacherName": "老师",
  "drillID": 0,
  "childList": [
    {
      "clockInState": 0,
      "sectionNum": "1",
      "courseBegin": "2019-02-09 00:00",
      "teacherName": "老师",
      "drillID": 0,
      "childList": [
        {
          "clockInState": 0,
          "sectionNum": "1",
          "courseBegin": "2019-02-09 00:00",
          "teacherName": "老师",
          "drillID": 0,
          "sectionId": 145,
          "sectionLevel": "3",
          "parentId": 131,
          "content": "章节看到了吗",
          "sectionName": "疾病类型",
          "courseDrillId": 10,
          "userSectionStats": 0,
          "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
        }
      ],
      "sectionId": 131,
      "sectionLevel": "2",
      "parentId": 55,
      "content": "day1内容",
      "sectionName": "day1",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
    }
  ],
  "sectionId": 55,
  "sectionLevel": "1",
  "parentId": 0,
  "content": "结合流行病学史、临床表现",
  "sectionName": "诊断",
  "courseDrillId": 10,
  "userSectionStats": 1,
  "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
},
{
  "clockInState": 0,
  "sectionNum": "2",
  "courseBegin": "2019-02-09 00:00:00.0",
  "teacherName": "老师",
  "drillID": 0,
  "childList": [
    {
      "clockInState": 0,
      "sectionNum": "14",
      "courseBegin": "2019-02-22 00:00",
      "teacherName": "老师",
      "drillID": 0,
      "childList": [
        {
          "sectionName": "打吊针",
          "clockInState": 0,
          "sectionNum": "1",
          "courseDrillId": 10,
          "courseBegin": "2019-02-22 00:00",
          "teacherName": "老师",
          "drillID": 0,
          "userSectionStats": 0,
          "sectionId": 210,
          "sectionLevel": "3",
          "courseCover": "courseCover/courseCover预防感冒1547003167675.png",
          "parentId": 209
        }
      ],
      "sectionId": 209,
      "sectionLevel": "2",
      "parentId": 207,
      "sectionName": "day1",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
    },
    {
      "clockInState": 0,
      "sectionNum": "15",
      "courseBegin": "2019-02-23 00:00",
      "teacherName": "老师",
      "drillID": 0,
      "childList": [
        {
          "sectionName": "吃西药",
          "clockInState": 0,
          "sectionNum": "1",
          "courseDrillId": 10,
          "courseBegin": "2019-02-23 00:00",
          "teacherName": "老师",
          "drillID": 0,
          "userSectionStats": 0,
          "sectionId": 402,
          "sectionLevel": "3",
          "courseCover": "courseCover/courseCover预防感冒1547003167675.png",
          "parentId": 236
        }
      ],
      "sectionId": 236,
      "sectionLevel": "2",
      "parentId": 207,
      "sectionName": "day2",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
    }
  ],
  "sectionId": 207,
  "sectionLevel": "1",
  "parentId": 0,
  "content": "undefined",
  "sectionName": "治疗",
  "courseDrillId": 10,
  "userSectionStats": 1,
  "courseCover": "courseCover/courseCover预防感冒1547003167675.png"
}
]       

如上找到每一项最后一级childList的数组,组成新的数组返回。

问题出现的环境背景及自己尝试过哪些方法

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

你期待的结果是什么?实际看到的错误信息又是什么?

alan 14
2019-01-11 提问
1 个回答
0

已采纳
/**
 * 
 * 递归遍历每棵树直到没有该节点没有子节点为止, 此时比较当前结点的 level 是否大于等于 maxLevel 的值,如 
 * 果是加入数组
 * 一棵树遍历完完毕,过滤掉小于 maxLevel 的节点,因为 maxLevel 值可能是递增的
 */

function find(data) {
  var maxLevel = 0,
    coll = [];
  for (let i = 0; i < data.length; i++) {
    let ret = [];
    maxLevel = 0;
    walk(data[i], ret, 1);
    coll.push(ret.filter(node => node.level >= maxLevel));
  }
/**
 * 
 * @param {Object} node 当前节点
 * @param {Array} ret 保存目标节点
 * @param {number} level 当前节点 level
 */
  function walk(node, ret, level) {
    if ((!node.childList || node.childList.length === 0 ) && level >= maxLevel) {
      node.level = level;
      ret.push(node);
      maxLevel = level;
      return;
    }
    for (let w of node.childList) {
      walk(w, ret, level + 1);
    }
  }
}
find(data);

撰写答案

推广链接