通过对象的嵌套数组进行递归循环以写入嵌套列表

时间:2019-03-04 05:17:13

标签: javascript html dom

考虑到嵌套的对象数组,我试图写一个嵌套的无序列表。

对数组本身进行组织,以便每个新的child属性都启动一个新的对象数组。该功能必须能够支持n级深度。

我当前的解决方案可以递归地编写我现在需要的所有属性,但是将

  • 标记附加在错误的
      上。

      我认为这是因为:

        var lowUL = targetUL.querySelector('ul');
      
      我在递归基本案例中也使用

      来添加

    • 。 它只会选择找到的第一个
        标签,而不会从for循环中的该迭代中动态创建的
          标签。

          // DATA
          const org1_depts = [
            {
              name: 'accounting',
              children: [
                { name: 'accounting payable', children: [] },
                { name: 'accounting receivable', children: [] },
              ],
            },
            {
              name: 'finance',
              children: [],
            },
          ]
          
          const org2_depts = [
            {
              name: 'accounting',
              children: [
                { name: 'accounting payable', children: [] },
                {
                  name: 'accounting receivable',
                  children: [{ name: 'cash', children: [] }, { name: 'check', children: [] }],
                },
              ],
            },
            {
              name: 'finance',
              children: [{ name: 'investment', children: [] }],
            },
          ]
          
          // FUNCTION
          
          function listOrg(orgData,targetUL) {
            var i;
            for (i = 0; i < orgData.length; i++) {
              if (orgData[i].hasOwnProperty('name')) {
                // Take out the text from the .name property
                var nameText = document.createTextNode(orgData[i].name);
                // Define a new <li> tag
                var newLI = document.createElement('li');
                // Append text to new <li> tage - newLI
                newLI.appendChild(nameText);
                // Append element to <ul> tag
                targetUL.appendChild(newLI);
              }
          
              if (orgData[i].hasOwnProperty('children')) {
                // Define new <ul> tag
                var newUL = document.createElement('ul');
                // Append new <ul> tag
                var lowUl = targetUL.appendChild(newUL);
                // Select new lower level <ul> tag
                var lowUL = targetUL.querySelector('ul');
                // Recurse
                listOrg(orgData[i].children,lowUL);
              }
            }
          }
          
          // CALL FUNCTION
          listOrg(org1_depts,document.getElementById("org1"));
          listOrg(org2_depts,document.getElementById("org2"));
          <!DOCTYPE html>
          <html lang="en">
          <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta http-equiv="X-UA-Compatible" content="ie=edge">
            <title>Document</title>
          </head>
          <body>
          <ul id='org1'>
            Organization 1
          </ul>
          
          <ul id='org2'>
            Organization 2
          </ul>
          
          </body>
          </html>

          “应收帐款”中的子名称属性被放在“应付帐款”中,这是错误的。

  • 1 个答案:

    答案 0 :(得分:3)

    当递归调用listOrg函数时,应将newUL变量作为参数而不是lowUL发送。 listOrg(orgData[i].children,newUL)这将针对新创建的ul,您无需使用querySelector

    // DATA
    const org1_depts = [
      {
        name: 'accounting',
        children: [
          { name: 'accounting payable', children: [] },
          { name: 'accounting receivable', children: [] },
        ],
      },
      {
        name: 'finance',
        children: [],
      },
    ]
    
    const org2_depts = [
      {
        name: 'accounting',
        children: [
          { name: 'accounting payable', children: [] },
          {
            name: 'accounting receivable',
            children: [{ name: 'cash', children: [] }, { name: 'check', children: [] }],
          },
        ],
      },
      {
        name: 'finance',
        children: [{ name: 'investment', children: [] }],
      },
    ]
    
    // FUNCTION
    
    function listOrg(orgData,targetUL) {
      var i;
      for (i = 0; i < orgData.length; i++) {
        if (orgData[i].hasOwnProperty('name')) {
          // Take out the text from the .name property
          var nameText = document.createTextNode(orgData[i].name);
          // Define a new <li> tag
          var newLI = document.createElement('li');
          // Append text to new <li> tage - newLI
          newLI.appendChild(nameText);
          // Append element to <ul> tag
          targetUL.appendChild(newLI);
        }
    
        if (orgData[i].hasOwnProperty('children')) {
          // Define new <ul> tag
          var newUL = document.createElement('ul');
          // Append new <ul> tag
          var lowUl = targetUL.appendChild(newUL);
          // Select new lower level <ul> tag
          var lowUL = targetUL.querySelector('ul');
          // Recurse
          listOrg(orgData[i].children,newUL );
        }
      }
    }
    
    // CALL FUNCTION
    listOrg(org1_depts,document.getElementById("org1"));
    listOrg(org2_depts,document.getElementById("org2"));
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
    </head>
    <body>
    <ul id='org1'>
      Organization 1
    </ul>
    
    <ul id='org2'>
      Organization 2
    </ul>
    
    </body>
    </html>