What about js traversing nested arrays?

data:[
        {
          title: "",
          isOpen: true,
          children: [
            {
              title: "",
              isOpen: true,
              children: [
                { 
                    title: "",
                    isOpen: false,
                },{ 
                    title: "",
                    isOpen: false,
                }
              ]
            },
            {
              title: "",
              isOpen: false,
              children: [
                {
                    title: "",
                    isOpen: false,
                }, {
                    title: "",
                    isOpen: false,
                }
              ]
            }
          ]
        },
        {
          title: "",
          isOpen: false,
          children: [
            {
                title: "",
                isOpen: false,
            }, {
                title: "",
                isOpen: false,
            }
          ]
        },
        {
          title: "",
          isOpen: false,
        }
      ]

the original data is as above
output rule:
if the upper isOpen:true, then process its children data, and put the children data into a separate array, insert it into the big tree group, and so on.
the basic output structure is as follows

[[,],[,],[,]]
Jan.11,2022

Recursive

A tree is formed according to your data structure, and the traversal method of the tree is used

  • Chestnut:

    
      let data=[/**/]
    
      let resultList=[]
      
      function deal(data){
        let temp=[]
        data.forEach(d => {
            temp.push(d.title)
            // 
            // data.forEach(d=>d.isOpen&&deal(d.children))
        });
        resultList.push(temp)
        // 
        data.forEach(d=>d.isOpen&&deal(d.children))
      }
    
      deal(data)
      console.log(resultList)   
  • effect

    clipboard.png

  • sort it out

    let data=[/**/]
    let resultList=[]
    function deal(data){
        let temp=[]
        data.forEach(({title}) => temp.push(title))
        resultList.push(temp)
        data.forEach(({isOpen,children})=>isOpen&&deal(children))
    }
    deal(data)
    console.log(resultList)  


let temporaryArray = [];

function filterData(data) {

    let title = data.map((item, index) => item.title);
    console.log(title);

    temporaryArray.push(title);

    let length = data.length;

    for(let i = 0; i < length; iPP) {
        if(data[i].isOpen) {
            filterData(data[i].children);
        }
    }

    return temporaryArray
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c2a4-2c31d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c2a4-2c31d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?