SoFunction
Updated on 2025-04-10

Analysis on the principle of implementation of JS traversal tree hierarchy relationship

1.Traveling the hierarchical relationship of the tree

1) Organize the data first

2) Find the mapping relationship between id and data

3) Then find the data of the parent node and store it

The code is as follows

test() {
   const list = [
    { id: "123", parentId: "", children: [] },
    { id: "124", parentId: "123", children: [] },
    { id: "125", parentId: "124", children: [] },
    { id: "126", parentId: "125", children: [] },
    { id: "127", parentId: "126", children: [] }
   ];
   const mapList = [];
   const tree = [];
   (item => {
    
    mapList[] = item;
   });
   (item => {
    const parentNode = mapList[];
    if (!parentNode) {

 if (!) {
  = []
 }

     (item);
    } else {

if (!) {
 = []
}

     (item);
    }
   });
   ("tree", tree);
  },

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.