Data filtering of array

there is a set of data that is connected through parent_id and has a dependency

[
    {
        id: 21,
        parent_id: 2
    },
    {
        id: 22,
        parent_id: 2
    }
]
Jul.21,2021

the problem of the subject has been solved.

but I still want to send out my own implementation. I saw the problem yesterday and have been paying attention to this problem, because this problem is consistent with the situation encountered in my project.
when defining data, the data given at the back end is flat, but you need to render it as a nested list when processing the data.
implements the idea of converting to a tree in the project, and implements this problem by the way, which can be regarded as a summary of the process.
according to their own implementation and ideas are as follows:

  1. The advantage of this step is that it is very easy to deal with node relationship nodes, and the business in my project is heavily dependent on node relationships, so it is converted to a tree because of the fact that the

    array (to build a flat dataset that relies on node data)-> tree structure (establishing a relationship is no longer dependent on node data) / / the advantage of this step is that it is very easy to deal with node relationship nodes.

    • corresponds to the transformTree method, where it should be noted that data that does not exist in the dataset is added, that is, the nodes in the tree do not exactly correspond to the data in the dataset.
  2. Tree structure finds nodes in the tree through a given id

    • corresponds to findNodeByIdFromTree method
  3. find the upper and lower nodes related to the current tree node from the tree

    • corresponds to findRelateNodesByIdFromTree method
    • look in the direction of the leaf node

      • through tree2nodes iteration
    • look for the root node

      • implement
      through findUpNodesById

among them, the function of 2Prior 3 is entirely due to the need of this topic, which is not needed in my project.

   

Menu