Forgot password
 Register account
View 142|Reply 0

嵌套集合转换为HTML列表

[Copy link]

3203

Threads

7841

Posts

48

Reputation

Show all posts

hbghlyj posted 2023-7-19 02:52 |Read mode
Last edited by hbghlyj 2023-7-19 03:11
  1. function convertTreeToHTML(tree) {
  2. let html = '<ul>';
  3. for (let i = 0; i < tree.length; i++) {
  4. html += '<li>';
  5. if (Array.isArray(tree[i]) && tree[i].length > 0) {
  6. html += convertTreeToHTML(tree[i]); // Recursive call
  7. }
  8. html += '</li>';
  9. }
  10. html += '</ul>';
  11. return html;
  12. }
Copy the Code
例子:集合$\left\{\left\{\left\{\right\},\left\{\{\}\right\},\left\{\right\}\right\},\left\{\left\{\right\},\left\{\right\}\right\}\right\}$
  1. html = convertTreeToHTML([[[],[[]],[]],[[],[]]]);
  2. console.log(html);
Copy the Code
输出
  1. <ul>
  2. <li>
  3. <ul>
  4. <li></li>
  5. <li>
  6. <ul>
  7. <li></li>
  8. </ul>
  9. </li>
  10. <li></li>
  11. </ul>
  12. </li>
  13. <li>
  14. <ul>
  15. <li></li>
  16. <li></li>
  17. </ul>
  18. </li>
  19. </ul>
Copy the Code

Quick Reply

Advanced Mode
B Color Image Link Quote Code Smilies
You have to log in before you can reply Login | Register account

$\LaTeX$ formula tutorial

Mobile version

2025-7-4 10:22 GMT+8

Powered by Discuz!

Processed in 0.035125 seconds, 23 queries