SOURCE

console.log(111);
let data = [{
  "childList": [{
    "childList": [],
    "deptName": "部门2",
    "fullName": "部门2阿",
    "id": 9,
    "isDeleted": 0,
    "parentId": 8,
    "remark": "mark",
    "sort": 0,
    "tenantCode": "-1",
    "key": ["8", "9"]
  }],
  "deptName": "部门1",
  "fullName": "部门1阿",
  "id": 8,
  "isDeleted": 0,
  "parentId": 0,
  "remark": null,
  "sort": 1,
  "tenantCode": "-1",
  "key": ["8"]
},
{
  "childList": [],
  "deptName": "aa",
  "fullName": "aa",
  "id": 26,
  "isDeleted": 0,
  "parentId": 0,
  "remark": null,
  "sort": 2,
  "tenantCode": "-1",
  "key": ["26"]
},
{
  "childList": [],
  "deptName": "bb",
  "fullName": "bb",
  "id": 27,
  "isDeleted": 0,
  "parentId": 0,
  "remark": "不不不不不",
  "sort": 0,
  "tenantCode": "-1",
  "key": ["27"]
}];

function loopSearch(array,id){
for (let i = 0; i < array.length; i++) {
  if (array[i].id == id) {
    return array[i].key;
  } else {
    let childLoop = loopSearch(array[i].childList, id);
    if (childLoop) {
      return childLoop;
    }
  }
}	
}


console.log(loopSearch(data, "27"));

console 命令行工具 X clear

                    
>
console