How to deal with the problem of foreach nesting of multi-dimensional arrays more elegantly

returns the corresponding value by comparing the names of provinces, cities and regions. It doesn"t seem feasible to use recursion or filter. Is there a short and elegant way to write it

getChinaArea(province,city,area){
                let result = []
                //
                if(!province){
                    this.localData.forEach((items)=>{
                        if(items.label === city){
                            result.push(items.value)
                            if(items.children){
                                items.children.forEach((item)=>{
                                    if(item.label === area){
                                        result.push(item.value)
                                    }
                                })
                            }
                        }
                    })
                }else{
                    //
                    this.localData.forEach((items)=>{
                        if(items.label === province){
                            result.push(items.value)
                            if(items.children){
                                items.children.forEach((item)=>{
                                    if(item.label === city){
                                        result.push(item.value)
                                        if(item.children){
                                            item.children.forEach((i)=>{
                                                if(i.label === area){
                                                    result.push(i.value)
                                                }
                                            })
                                        }
                                    }
                                })
                            }
                        }
                    })
                }
                return result
            }

the data structure is like this

{
        value: 3,
        label: "",
        children: [{
                value: 3,
                label: "",
                children: [{
                        value: 37,
                        label: ""
                    },
                    {
                        value: 38,
                        label: ""
                    },
                    {
                        value: 39,
                        label: ""
                    },
                    {
                        value: 40,
                        label: ""
                    },
                    {
                        value: 41,
                        label: ""
                    },
                    {
                        value: 42,
                        label: ""
                    },
                    {
                        value: 43,
                        label: ""
                    },
                    {
                        value: 44,
                        label: ""
                    },
                    {
                        value: 45,
                        label: ""
                    },
                    {
                        value: 46,
                        label: ""
                    },
                    {
                        value: 47,
                        label: ""
                    },
                    {
                        value: 48,
                        label: ""
                    },
                    {
                        value: 49,
                        label: ""
                    },
                    {
                        value: 50,
                        label: ""
                    },
                    {
                        value: 51,
                        label: ""
                    },
                    {
                        value: 52,
                        label: ""
                    },
                    {
                        value: 53,
                        label: ""
                    },
                    {
                        value: 54,
                        label: ""
                    },
                    {
                        value: 55,
                        label: ""
                    },
                    {
                        value: 56,
                        label: ""
                    },
                    {
                        value: 57,
                        label: ""
                    },
                    {
                        value: 58,
                        label: ""
                    },
                    {
                        value: 59,
                        label: ""
                    }
                ]
            },
Jun.23,2022

it is recommended to convert the data to


using map.


create an object for a tree.

function Node(data) {
    this.data = data;
    this.parent = null;
    this.children = [];
}
 
function Tree(data) {
    var node = new Node(data);
    this._root = node;
}

create five more methods
1.traverseDF (callback) depth first search
2.traverseBF (callback) breadth first search
3.contains (data, traversal) query specified value
4.add (child, parent) add node
5.remove (node, parent) delete node


wrap the original data into an array to ensure the consistency of the parent-child data, and then recursively:

const res = [];
const getChinaArea2 = (dataArray, ...labels) => {
    const layer = labels.shift();
    if (!layer) return;
    const result = dataArray.find(one => one.label === layer);
    if (result) {
        res.push(result.value);
        return result.children ? getChinaArea2(result.children, ...labels) : undefined;
    };
};

learn from the code of Donle, encapsulate res into a function and have its own function scope

  function findChild (data, list, res = []) {
    let info = list.shift()
    if (!info) return res
    let dataInfo = data.find(item => item.label === info)
    res.push(dataInfo.value)
    return dataInfo.children ? findChild(dataInfo.children, list, res) : res
  }
  console.log(findChild(localData, ['', '', '']))
  console.log(findChild(localData, ['', '']))

function getVal(f,d){
  for(var i=0,newArr = [];i<d.length;iPP){
    if(d[i].label==f[0]){
      newArr.push(d[i]);
      f.shift();
      if(d[i]&&d[i].children&&f.length!=0){
        if(newArr.length==0)
          newArr[0].children.push(getVal(f,d[i].children));
        else
          newArr[0].children = getVal(f,d[i].children);
      }
    }
  }
  return newArr;
}
getVal([''],data);

  

convert to map type

Menu