js 查找树种某一条数据 最深层的子数据(递归获取指定某一条数据)

var data = {
    'code': 200,
    'data': [
        {
        'cascadeIds': 'f24e3b3b31ea11ea8bef000c29aefb8e_6c5feaf031c111ea8bef000c29aefb8e_17fca55631c711ea8bef000c29aefb8e',
        'childTree': [
            {
            'cascadeIds': '',
            'childTree': [
                {
                "cascadeIds": "",
                "childTree": [
                    {
                        "cascadeIds": "",
                        "childTree": [
                            {
                                "cascadeIds": "",
                                "childTree": [],
                                "id": "6c67349931c111ea8bef000c29aefb8e",
                                "masterRate": 0,
                                "name": "集合的概念",
                                "parentId": "6c65915431c111ea8bef000c29aefb8e",
                                "scoringRate": 0,
                                "sort": 2,
                                "topicCount": 0,
                                "topicFinishCount": 0,
                                "type": 0
                            }
                        ],
                        "id": "6c65915431c111ea8bef000c29aefb8e",
                        "masterRate": 0,
                        "name": "集合的含义与表示",
                        "parentId": "6c63e0d231c111ea8bef000c29aefb8e",
                        "scoringRate": 0,
                        "sort": 2,
                        "topicCount": 0,
                        "topicFinishCount": 0,
                        "type": 0
                    }
                ],
                "id": "6c63e0d231c111ea8bef000c29aefb8e",
                "masterRate": 0,
                "name": "集合",
                "parentId": "6c62574a31c111ea8bef000c29aefb8e",
                "scoringRate": 0,
                "sort": 2,
                "topicCount": 0,
                "topicFinishCount": 0,
                "type": 0
            }],
        'id': '17fca55631c711ea8bef000c29aefb8e',
        'masterRate': 0,
        'name': '数学核心素养',
        'parentId': '6c5feaf031c111ea8bef000c29aefb8e',
        'scoringRate': 0,
        'sort': 1,
        'topicCount': 0,
        'topicFinishCount': 0,
        'type': 0
    },
    'error': '',
    'msg': 'sucess'
}

getCurrentTree (data.data)

const getCurrentTree = ( chapterTree ) => {
  if( isArray(chapterTree) && chapterTree.length>0 && chapterTree[0].childTree && chapterTree[0].childTree.length>0 ){
    return getCurrentTree(chapterTree[0].childTree);
  }else{
    return chapterTree[0];
  }
};

function getCurrentTree (treeData){
    if (treeData.childTree[0].childTree.length > 0) {
        return getCurrentTree(treeData.childTree[0])
    } else {
        return treeData.childTree[0]
    }
}

你可能感兴趣的:(js 查找树种某一条数据 最深层的子数据(递归获取指定某一条数据))