Js N-layer array nests the last layer of data for Filter

problem description

as shown in the title, the data obtained at present is a multi-layer tree structure, and each layer has its own children,. How can you get the children of the last layer to deal with it, and carry out Filter to process the data? only Avatar is the last layer of the processed data. Like the second group of data, the number of layers is not fixed. Ask the bosses for advice on how to deal with this kind of data.

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)

before modification:
[{

]
"code": "001",
"agyTypeCode": "2",
"children": [{
    "code": "001001",
    "agyTypeCode": "1",
    "children": [{
        "code": "001001001",
        "agyTypeCode": "1",
        "children": [{
            "finChfName": "",
            "isPairAc": 1,
            "code": "0001",
            "name": "2019",
            "agyCode": "001001001",
            "id": "f28dad108fd411e89b13a146c4299200",
            "acsCode": "001"
        }, {
            "finChfName": "",
            "isPairAc": 0,
            "code": "0002",
            "name": "",
            "agyCode": "001001001",
            "id": "e66f85a1907211e88798292b71e6b4b4",
            "acsCode": "002"
        }, {
            "finChfName": "",
            "isPairAc": 1,
            "code": "0003",
            "name": "",
            "agyCode": "001001001",
            "id": "f0cf4d60963911e8ba520dc161e32ad2",
            "acsCode": "001"
        }],
        "name": "",
        "pid": "41a9b5c08fd411e89e1bcd37e4564ff2",
        "id": "627df2208fd411e89e1bcd37e4564ff2"
    }],
    "name": "",
    "pid": "0d2ea6c08fd411e89e1bcd37e4564ff2",
    "id": "41a9b5c08fd411e89e1bcd37e4564ff2"
}],
"name": "",
"pid": "",
"id": "0d2ea6c08fd411e89e1bcd37e4564ff2"

}]
modified:
[{

"code": "001",
"agyTypeCode": "2",
"children": [{
    "code": "001001",
    "agyTypeCode": "1",
    "children": [{
        "code": "001001001",
        "agyTypeCode": "1",
        "children": [ {
            "finChfName": "",
            "isPairAc": 0,
            "code": "0002",
            "name": "",
            "agyCode": "001001001",
            "id": "e66f85a1907211e88798292b71e6b4b4",
            "acsCode": "002"
        }],
        "name": "",
        "pid": "41a9b5c08fd411e89e1bcd37e4564ff2",
        "id": "627df2208fd411e89e1bcd37e4564ff2"
    }],
    "name": "",
    "pid": "0d2ea6c08fd411e89e1bcd37e4564ff2",
    "id": "41a9b5c08fd411e89e1bcd37e4564ff2"
}],
"name": "",
"pid": "",
"id": "0d2ea6c08fd411e89e1bcd37e4564ff2"

}]

Apr.11,2021

this scenario uses recursive solutions

function processLastChildrens(arr) {
    for(var i in arr) {
        var obj = arr[i];
        if(!obj.children) {
            continue;
        }
        var findResult = processLastChildrens(obj.children);
        if(!findResult) { //children
            obj.children = obj.children.filter(o=>o.finChfName === '');
            findResult = true;
        } 
    }
    return findResult;
}

var newArr = [].concat(arr); //

processLastChildrens(newArr);
console.log(newArr); //
Menu