Flat tree structure data array transformed into

Before the back-end data structures are responsible for providing the assembly, Taikang here because it is IBM, the tree structure of the data provided by their side is flat, so they turn themselves into a tree structure desired data

Background is generally to data, PANNT its corresponding parent element id

const data = [
      { FLAG: "", HR_CJ: "01", PANNT: "00001000", ROOTID: "00001001" },
      { FLAG: "", HR_CJ: "02", PANNT: "00001001", ROOTID: "00001002" },
      { FLAG: "", HR_CJ: "03", PANNT: "00001002", ROOTID: "00001003" },
      { FLAG: "", HR_CJ: "04", PANNT: "00001003", ROOTID: "00001004" }
    ]

The method of encapsulation and conversion data are as follows:

createTreeData ({ 
      List = [], 
      ID = "ID" , 
      the parentId = "parent_id" , 
      rooId = 0 
    }) { 
      const Tree = list.filter (Father => {
         // cycle all 
        const = fatherCopy Father; 
        const branchArr = list.filter ( 
          child => fatherCopy [ID] === child [the parentId] 
        ); // returns the number of children of each group 
        IF (branchArr.length> 0 ) { 
          fatherCopy.children = branchArr; //If there is a child, a parent is added to the children property, and assignment 
        }
         return fatherCopy [the parentId] === rooId; // return a first layer 
      });
       return Tree; // return tree data 
    }

The end result is that we want the tree structure:

 

Guess you like

Origin www.cnblogs.com/zpxm/p/10983635.html