Forgot password?
 Create new account
View 94|Reply 0

嵌套集合转换为HTML列表

[Copy link]

3147

Threads

8493

Posts

610K

Credits

Credits
66163
QQ

Show all posts

hbghlyj Posted at 2023-7-19 02:52:26 |Read mode
Last edited by hbghlyj at 2023-7-19 03:11:00
  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

手机版Mobile version|Leisure Math Forum

2025-4-20 22:25 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list