10,000 pieces of data were lost in the background No matter how much I planned, I still couldn't escape it. The backend really threw tens of thousands of data to the frontend at once. Well, at least it’s not 100,000 yet. As shown below, the background returns the following structure: const flatArr = [ { id: '001', name: 'Node 1' }, { id: '0011', parentId: '001', name: 'Node 1-1' }, { id: '00111', parentId: '0011', name: 'Node 1-1-1' }, { id: '002', name: 'Node 2' }, ] As you can see, this is actually a flat array. Our requirement is to render such data in the cascade selector of Element-ui, which receives the following tree structure: let options = [ { value: 'zhinan', label: 'Guide', children: [ { value: 'shejiyuanze', label: 'Design principles', children: [ { value: 'yizhi', label: 'consistent' }, { value: 'fankui', label: 'Feedback'} ], } ] } ] Oh my god, this requires me to convert the flat array into a tree structure! Recursive method It’s done, the code is concise, the idea is clear, and when it runs, what? The page is stuck. According to console.time(), it took about 18 seconds to calculate the tree structure we need. Non-recursive method Here, we cleverly apply the feature that objects store references. Each time, the id of the current node is used as the key to save the reference information of the corresponding node. When traversing the array, the children information of objMap is updated each time. In this way, all nodes and their child nodes are retained in objMap. Most importantly, we only need to traverse the array once, and the time complexity is O(n). Using this method, the calculation time only takes 60ms! Summarize
Let's look at a comparison chart: From the above trend of time complexity increasing with the amount of data, we can see that when the data becomes larger and larger, the time consumed by the recursive algorithm will be much greater than that of the non-recursive algorithm. Therefore, when the amount of data is small, using a recursive algorithm has certain advantages, but when the data is large to a certain extent, the disadvantages of the recursive approach will become more and more obvious, and using a non-recursive algorithm will be much faster! Finally, I have to say that through this comparison, we can clearly feel the importance of algorithms. Different implementation methods can have huge differences, which deserves the attention of every developer! This is the end of this article about the implementation example of converting JavaScript flat array to tree structure. For more relevant content about converting JavaScript flat array to tree structure, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Common causes and solutions for slow MySQL SQL statements
>>: MYSQL stored procedures, that is, a summary of common logical knowledge points
concept MMM (Master-Master replication manager fo...
The PHP base image used in this article is: php:7...
Table of contents The server runs jupyter noteboo...
Table of contents Overview Problem Description Ci...
1. Problem reproduction: Count the total number o...
Two ways to navigate the page Declarative navigat...
environment name property CPU x5650 Memory 4G dis...
engine Introduction Innodb engine The Innodb engi...
Preface When I went to an interview at a company ...
MySQL is a relational database management system ...
The effect is as follows: Example 1 Example 2: Ta...
Does color influence website visitors? A few year...
I recently upgraded MySQL to 5.7, and WordPress r...
This article describes the linux system commands....
The difference between replace into and insert in...