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
}
Menu