o2h

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
"<ul>
  <li>index.html</li>
  <li>js<ul>
      <li>index.js</li>
    </ul>
  </li>
  <li>css<ul>
      <li>index.css</li>
    </ul>
  </li>
</ul>"

<body>
  <script>
    var flag = 0;
    function o2h(o) {
      var tmp = "<ul>";

      for (let index = 0; index < o.length; index++) {
        if (flag !== 0 && flag === index) {
          continue
        }
        if (index >= o.length) {
          tmp += '<li>' + o[index] + '</li>';
          return tmp;
        }
        if (typeof o[index + 1] === 'object') {
          flag = index + 1;
          tmp += '<li>' + o[index] + o2h(o[index + 1]) + '</li>';
        } else {
          tmp += '<li>' + o[index] + '</li>';
        }
      }
      tmp += "</ul>"
      +
      return tmp;
    }

    var tmp = [
      'index.html',
      'js', ['index.js'],
      'css', ['index.css']
    ]
    console.dir(o2h(tmp))

  </script>
</body>

</html>
View Code

 

posted @ 2021-08-24 09:15  酒觉  阅读(65)  评论(0编辑  收藏  举报