在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ js N層數(shù)組嵌套取最后一層數(shù)據(jù)進行過濾

js N層數(shù)組嵌套取最后一層數(shù)據(jù)進行過濾

問題描述

如題所示,目前獲取到的數(shù)據(jù)是多層樹形結(jié)構(gòu),每一層都有自己的children,如何才能獲取到最后一層的children對其進行處理,將數(shù)據(jù)進行過濾,處理后的數(shù)據(jù)最后一層只有阿凡達這一組,如第二組數(shù)據(jù)那樣 ,層數(shù)是不固定的,求大佬們指點如何處理該種數(shù)據(jù)?

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

相關(guān)代碼

// 請把代碼文本粘貼到下方(請勿用圖片代替代碼)

修改前:
[{

"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": "華南大區(qū)匯總",
"pid": "",
"id": "0d2ea6c08fd411e89e1bcd37e4564ff2"

}]
修改后:
[{

"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": "華南大區(qū)匯總",
"pid": "",
"id": "0d2ea6c08fd411e89e1bcd37e4564ff2"

}]

回答
編輯回答
你好胸

這種場景使用遞歸解決

function findLastChildrens(arr) {
    var result = [];

    for(var i in arr) {
        var obj = arr[i];
        if(!obj.children) {
            continue;
        }
        var findResult = findLastChildrens(obj.children);
        if(findResult.length === 0) { //當前children其下再沒有children出現(xiàn),說明當前children是最后一級
            result.push(obj.children);
        } else { //不是最后一級children的話
            result = result.concat(findResult);
        }
    }
    return result;
}

findLastChildrens(arr) //返回你要所有最后一級的children組成的數(shù)組

題主改了需求,調(diào)整后的解決方案

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); //避免對原數(shù)組產(chǎn)生影響,克隆一個數(shù)組

processLastChildrens(newArr);
console.log(newArr); //打印出你期望的新數(shù)組
2017年3月13日 20:22