How to use js recursion to get child node data down infinitely so as to get a new tree data?

existing data:

var res = [{
    code: 1,
    name: "",
    children: [{
        code: 1,
        name: "",
        children: [{
            code: 1,
            name: "",
            children: [{
                code: 1,
                name: ""
            }]
        }, {
            code: 1,
            name: "",
            children: [{
                code: 1,
                name: ""
            }]
        }, {
            code: 1,
            name: "",
            children: [{
                code: 1,
                name: ""
            }]
        }]
    }, {
        code: 1,
        name: "",
        children: [{
            code: 1,
            name: "",
            children: [{
                code: 1,
                name: ""
            }]
        }, {
            code: 1,
            name: "",
            children: [{
                code: 1,
                name: ""
            }]
        }]
    }]
}]

want to recursively get the data in the following format (that is, some fields that Filter does not need):

var newData = [{
    name: "",
    children: [{
        name: "",
        children: [{
            name: "",
            children: [{
                name: ""
            }]
            ...
        }]
    },{
        name: "",
        children: [{
            name: "",
            children: [{
                name: ""
            }]
            ...
        }]
    }]
}]

now I can only use nested loops to solve the problem. If you want to simplify the code or don"t know how many layers there are, nested loops certainly won"t work. How can I implement them with recursion?

Apr.09,2021

function serverArray(arr){
  for(var item = 0;item < arr.length;itemPP){
    if(arr[item].children){
      delete arr[item].code
      serverArray(arr[item].children)
    }
  }
  return arr
}
console.log(serverArray(res))
Is that what

means?


must refute "loops don't work"
function s(){
    let n = res;
    let r = [...n];
    do{
        let N_r = [];
        for(let node of r){
            delete(node["code"]);
            if(node.children){
                N_r = N_r.concat(node.children);
            }
        }
        r = N_r;
    }while(r.length);
    return n; 
}

< script type= "text/javascript" >

var json=[{
    "id": "100",
    "name": "",
    "children":[{
        "id": "1100",
        "name": "",
        "children":[{
            "id": "11001",
            "name": "",
            
        },{
            "id": "11002",
            "name": "",
            
        },{
            "id": "11003",
            "name": "",
            
        }]
    }]
  }
  ,{
    "id": "200",
    "name": "",
    "children":[{
        "id": "2100",
        "name": "",
        "children":[{
            "id": "21001",
            "name": "",
            "children":[{
                "id": "23001",
                "name": "",
            },{
                "id": "23002",
                "name": "",
            }]
        },{
            "id": "21002",
            "name": "",
            "children":[{
                "id": "22001",
                "name": "",
            },{
                "id": "22002",
                "name": "",
            }]
        }]
    }]
  }
  ];

/ / get the last child of each object in the array, provided that if there is no value in children, you cannot put an empty "children": []

 var arr1 = []
 function serverArray(arr){
  for(var item = 0;item < arr.length;itemPP){

    if(arr[item].children){
      serverArray(arr[item].children)
    }else{
        arr1.push(arr[item])
    }
  }
  return arr1
}
console.log(serverArray(json))

< / script >
data:

  1. (7) [{ }, {. }, {. }, {. }, {. }, {. }, {. }
  2. 0: {id: "11001", name: "Xihu District"}
  3. 1: {id: "11002", name: "Upper District"}
  4. 2: {id: "11003", name: "Lower Town"}
  5. 3: {id: "23001", name: "Xiaoluo"}
  6. 4: {id: "23002", name: "Grand Luo"}
  7. 5: {id: Name: "Tiananmen"}
  8. 6: {id: "22002", name: "Red Flag"}
  9. length: 7
  10. _ proto__: Array (0)
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-1b386da-2c129.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-1b386da-2c129.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?