1、根据id查询当前对象

//list:遍历的对象,id:查询所需的id
function getObjById(list, id) {
    if (!list instanceof Array) return null;
    for(let i in list){
        let item = list[i];
        if(item.id === id) return item;
        else {
            if(item.children) {
                let obj = getObjById(item.children, id);
                if(obj) return obj
console.log(getObjById(cityList, '1-1-2')); //{ id: '1-1-2', name: '金牛区' } 

2、根据id查询当前对象及所有父级节点对象

function getParentsById(list, id) {
    for (let i in list) {
        let item = list[i];
        if (item.id === id) return [item];
        if (item.children) {
            let node = getParentsById(item.children, id)
            if (node !== undefined) return node.concat(item)
console.log(getParentsById(cityList,'1-1-2'))

3、根据id查询当前对象及所有子级节点对象

function getChildren(list, returnValue = []) {
    for (let i in list) {
        let item = list[i];
        returnValue.push(item)
        if (item.children) {
            getChildren(item.children, returnValue)