Js recursively finds the last level array and returns it as a new array.

     [
{
  "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/courseCover1547003167675.png"
        }
      ],
      "sectionId": 131,
      "sectionLevel": "2",
      "parentId": 55,
      "content": "day1",
      "sectionName": "day1",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover1547003167675.png"
    }
  ],
  "sectionId": 55,
  "sectionLevel": "1",
  "parentId": 0,
  "content": "",
  "sectionName": "",
  "courseDrillId": 10,
  "userSectionStats": 1,
  "courseCover": "courseCover/courseCover1547003167675.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/courseCover1547003167675.png",
          "parentId": 209
        }
      ],
      "sectionId": 209,
      "sectionLevel": "2",
      "parentId": 207,
      "sectionName": "day1",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover1547003167675.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/courseCover1547003167675.png",
          "parentId": 236
        }
      ],
      "sectionId": 236,
      "sectionLevel": "2",
      "parentId": 207,
      "sectionName": "day2",
      "courseDrillId": 10,
      "userSectionStats": 1,
      "courseCover": "courseCover/courseCover1547003167675.png"
    }
  ],
  "sectionId": 207,
  "sectionLevel": "1",
  "parentId": 0,
  "content": "undefined",
  "sectionName": "",
  "courseDrillId": 10,
  "userSectionStats": 1,
  "courseCover": "courseCover/courseCover1547003167675.png"
}
]       

find the array of each last level childList as above, and return it as a new array.

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Apr.18,2022

/**
 * 
 *   level  maxLevel  
 * 
 *  maxLevel  maxLevel 
 */

function find(data) {
  var maxLevel = 0,
    coll = [];
  for (let i = 0; i < data.length; iPP) {
    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);

function treeToArray (data) {

const result = [];
data.forEach(item => {// 
    const loop = data => {
        let child = data.childList;
        if(child){// 
            for(let i = 0; i < child.length; iPP){
                loop(child[i]);
            }
        } else {
            result.push(data);
        }
    }
    loop(item);
})
return result;

}

Menu