Javascript使用递归从给定数组生成数组

标签 javascript arrays recursion

假设我有一个 API 调用,它为我提供了三个对象的结果。

我生成的示例数据位于:https://pastebin.com/TSPLP8yf

我如何在 JavaScript 中生成完整的数组列表?

function generateNewSectorList(sectors, newList) {

    for (var i = 0; i < sectors.length; i++){
        var obj = sectors[i];
        newList.push(obj);
        if (hasChildren(obj)) {
            for (var j = 0; j < obj.Children.length; j++) {
                newList = generateNewSectorList(obj.Children[j], newList);
            }
        }

    }

    return newList;
}

这就是我现在所处的位置。

首先使用该 Pastebin 链接上的数据和空数组 ( [] ) 进行初始化。

函数 hasChildren 如下:

    function hasChildren(obj) {
    if (obj.Children.length === 0) {
        return false;
    }
    return true;
}

最后,我需要该列表,以便每当有子元素时,与它的父元素相比,它的名称前面应该有一个额外的  

元素已在后端按正确的顺序排序。

使用递归的原因是,现在有 3 层元素:Parent、Children 和 GrandChildren,但我希望应用程序即使在有 4 层或更多层时也能正常工作。

如果没有第四层,我就不会使用递归,而只在方法中使用三个 for 循环。

最佳答案

"In the end i need that list to be so that whenever there is a children element, it's name should have an extra &nbsp; in front of it compared to it's parent."

您需要做的就是传入一个字符串,并在每次递归调用时添加到该字符串中。

编辑:我错过了您错误地处理递归。该函数需要一个数组,因此这就是您需要传递的内容。不需要内循环。

function generateNewSectorList(sectors, newList, indent) {
    for (var i = 0; i < sectors.length; i++){
        var obj = sectors[i];

        // I guess this is where you want the indentation?
        obj.Name = indent + obj.Name;

        newList.push(obj);

        if (hasChildren(obj)) {
            newList = generateNewSectorList(
                          obj.Children,
                          newList,
                          indent + "&nbsp;" // Increase the indentation
                        );
            }
        }

    }

    return newList;
}

因此,第一次调用将传递一个空字符串,或者您想要开始的任何缩进量。

generateNewSectorList(data, [], "");

这是一个演示:

setTimeout(function() {
  const div = document.querySelector("div");
  const res = generateNewSectorList(data, [], "");
  res.forEach(obj => div.insertAdjacentHTML("beforeend", obj.Name + "<br>"))
}, 100);


function generateNewSectorList(sectors, newList, indent) {
  for (var i = 0; i < sectors.length; i++) {
    var obj = sectors[i];

    // I guess this is where you want the indentation?
    obj.Name = indent + obj.Name;

    newList.push(obj);

    if (hasChildren(obj)) {
      newList = generateNewSectorList(
        obj.Children,
        newList,
        indent + "&nbsp;" // Increase the indentation
      );
    }

  }

  return newList;
}

function hasChildren(obj) {
  if (obj.Children.length === 0) {
    return false;
  }
  return true;
}


var data = [{
    "Children": [{
        "Children": [],
        "Forms": [],
        "SectorId": 4,
        "Name": "Construction materials",
        "ParentSectorId": 1
      },
      {
        "Children": [],
        "Forms": [],
        "SectorId": 5,
        "Name": "Electronics and Optics",
        "ParentSectorId": 1
      },
      {
        "Children": [{
            "Children": [],
            "Forms": [],
            "SectorId": 23,
            "Name": "Bakery & confectionery products",
            "ParentSectorId": 6
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 24,
            "Name": "Beverages",
            "ParentSectorId": 6
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 25,
            "Name": "Fish & fish products",
            "ParentSectorId": 6
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 26,
            "Name": "Meat & meat products",
            "ParentSectorId": 6
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 27,
            "Name": "Milk & dairy products",
            "ParentSectorId": 6
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 28,
            "Name": "Other (Food)",
            "ParentSectorId": 6
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 29,
            "Name": "Sweets & snack food",
            "ParentSectorId": 6
          }
        ],
        "Forms": [],
        "SectorId": 6,
        "Name": "Food and Beverage",
        "ParentSectorId": 1
      },
      {
        "Children": [{
            "Children": [],
            "Forms": [],
            "SectorId": 30,
            "Name": "Bathroom/sauna",
            "ParentSectorId": 7
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 31,
            "Name": "Bedroom",
            "ParentSectorId": 7
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 32,
            "Name": "Children’s room",
            "ParentSectorId": 7
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 33,
            "Name": "Kitchen",
            "ParentSectorId": 7
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 34,
            "Name": "Living room",
            "ParentSectorId": 7
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 35,
            "Name": "Office",
            "ParentSectorId": 7
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 36,
            "Name": "Other (Furniture)",
            "ParentSectorId": 7
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 37,
            "Name": "Outdoor",
            "ParentSectorId": 7
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 38,
            "Name": "Project furniture",
            "ParentSectorId": 7
          }
        ],
        "Forms": [],
        "SectorId": 7,
        "Name": "Furniture",
        "ParentSectorId": 1
      },
      {
        "Children": [{
            "Children": [],
            "Forms": [],
            "SectorId": 39,
            "Name": "Machinery components",
            "ParentSectorId": 8
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 40,
            "Name": "Machinery equipment/tools",
            "ParentSectorId": 8
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 41,
            "Name": "Manufacture of machinery",
            "ParentSectorId": 8
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 42,
            "Name": "Maritime",
            "ParentSectorId": 8
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 43,
            "Name": "Aluminium and steel workboats",
            "ParentSectorId": 8
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 44,
            "Name": "Boat/Yacht building",
            "ParentSectorId": 8
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 45,
            "Name": "Ship repair and conversion",
            "ParentSectorId": 8
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 46,
            "Name": "Metal structures",
            "ParentSectorId": 8
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 47,
            "Name": "Other (Machinery)",
            "ParentSectorId": 8
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 48,
            "Name": "Repair and maintenance service",
            "ParentSectorId": 8
          }
        ],
        "Forms": [],
        "SectorId": 8,
        "Name": "Machinery",
        "ParentSectorId": 1
      },
      {
        "Children": [{
            "Children": [],
            "Forms": [],
            "SectorId": 49,
            "Name": "Construction of metal structures",
            "ParentSectorId": 9
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 50,
            "Name": "Houses and buildings",
            "ParentSectorId": 9
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 51,
            "Name": "Metal products",
            "ParentSectorId": 9
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 52,
            "Name": "Metal works",
            "ParentSectorId": 9
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 53,
            "Name": "CNC-machining",
            "ParentSectorId": 9
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 54,
            "Name": "Forgings, Fasteners",
            "ParentSectorId": 9
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 55,
            "Name": "Gas, Plasma, Laser cutting",
            "ParentSectorId": 9
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 56,
            "Name": "MIG, TIG, Aluminum welding",
            "ParentSectorId": 9
          }
        ],
        "Forms": [],
        "SectorId": 9,
        "Name": "Metalworking",
        "ParentSectorId": 1
      },
      {
        "Children": [{
            "Children": [],
            "Forms": [],
            "SectorId": 57,
            "Name": "Packaging",
            "ParentSectorId": 10
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 58,
            "Name": "Plastic goods",
            "ParentSectorId": 10
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 59,
            "Name": "Plastic processing technology",
            "ParentSectorId": 10
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 60,
            "Name": "Blowing",
            "ParentSectorId": 10
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 61,
            "Name": "Moulding",
            "ParentSectorId": 10
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 62,
            "Name": "Plastics welding and processing",
            "ParentSectorId": 10
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 63,
            "Name": "Plastic profiles",
            "ParentSectorId": 10
          }
        ],
        "Forms": [],
        "SectorId": 10,
        "Name": "Plastic and Rubber",
        "ParentSectorId": 1
      },
      {
        "Children": [{
            "Children": [],
            "Forms": [],
            "SectorId": 64,
            "Name": "Advertising",
            "ParentSectorId": 11
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 65,
            "Name": "Book/Periodicals printing",
            "ParentSectorId": 11
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 66,
            "Name": "Labelling and packaging printing",
            "ParentSectorId": 11
          }
        ],
        "Forms": [],
        "SectorId": 11,
        "Name": "Printing",
        "ParentSectorId": 1
      },
      {
        "Children": [{
            "Children": [],
            "Forms": [],
            "SectorId": 67,
            "Name": "Clothing",
            "ParentSectorId": 12
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 68,
            "Name": "Textile",
            "ParentSectorId": 12
          }
        ],
        "Forms": [],
        "SectorId": 12,
        "Name": "Textile and Clothing",
        "ParentSectorId": 1
      },
      {
        "Children": [{
            "Children": [],
            "Forms": [],
            "SectorId": 69,
            "Name": "Other (Wood)",
            "ParentSectorId": 13
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 70,
            "Name": "Wooden building materials",
            "ParentSectorId": 13
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 71,
            "Name": "Wooden houses",
            "ParentSectorId": 13
          }
        ],
        "Forms": [],
        "SectorId": 13,
        "Name": "Wood",
        "ParentSectorId": 1
      }
    ],
    "Forms": [],
    "Parent": null,
    "SectorId": 1,
    "Name": "Manufacturing",
    "ParentSectorId": null
  },
  {
    "Children": [{
        "Children": [],
        "Forms": [],
        "SectorId": 14,
        "Name": "Business Services",
        "ParentSectorId": 2
      },
      {
        "Children": [],
        "Forms": [],
        "SectorId": 15,
        "Name": "Engineering",
        "ParentSectorId": 2
      },
      {
        "Children": [],
        "Forms": [],
        "SectorId": 16,
        "Name": "Tourism",
        "ParentSectorId": 2
      },
      {
        "Children": [],
        "Forms": [],
        "SectorId": 17,
        "Name": "Translation Services",
        "ParentSectorId": 2
      },
      {
        "Children": [{
            "Children": [],
            "Forms": [],
            "SectorId": 72,
            "Name": "Data processing, Web portals, E-marketing",
            "ParentSectorId": 18
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 73,
            "Name": "Programming, Consultancy",
            "ParentSectorId": 18
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 74,
            "Name": "Software, Hardware",
            "ParentSectorId": 18
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 75,
            "Name": "Telecommunications",
            "ParentSectorId": 18
          }
        ],
        "Forms": [],
        "SectorId": 18,
        "Name": "Information Technology and Telecommunications",
        "ParentSectorId": 2
      },
      {
        "Children": [{
            "Children": [],
            "Forms": [],
            "SectorId": 76,
            "Name": "Air",
            "ParentSectorId": 19
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 77,
            "Name": "Rail",
            "ParentSectorId": 19
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 78,
            "Name": "Road",
            "ParentSectorId": 19
          },
          {
            "Children": [],
            "Forms": [],
            "SectorId": 79,
            "Name": "Water",
            "ParentSectorId": 19
          }
        ],
        "Forms": [],
        "SectorId": 19,
        "Name": "Transport and Logistics",
        "ParentSectorId": 2
      }
    ],
    "Forms": [],
    "Parent": null,
    "SectorId": 2,
    "Name": "Service",
    "ParentSectorId": null
  },
  {
    "Children": [{
        "Children": [],
        "Forms": [],
        "SectorId": 20,
        "Name": "Creative Industries",
        "ParentSectorId": 3
      },
      {
        "Children": [],
        "Forms": [],
        "SectorId": 21,
        "Name": "Energy technology",
        "ParentSectorId": 3
      },
      {
        "Children": [],
        "Forms": [],
        "SectorId": 22,
        "Name": "Environment",
        "ParentSectorId": 3
      }
    ],
    "Forms": [],
    "Parent": null,
    "SectorId": 3,
    "Name": "Other",
    "ParentSectorId": null
  }
];
<div></div>

关于Javascript使用递归从给定数组生成数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46624846/

相关文章:

javascript - Jquery 模式无法正常工作

javascript - 鼠标移动时移动 div

javascript - 如何根据特定的 JSON 对象值对数组进行计数?

arrays - 在每第 n 个元素之后填充数组

postgresql - 在 postgresql 中查询递归表和其他表?

java - 查找数组中有多少个不同的值

javascript - window.onbeforeunload 和 window.onunload 在 Firefox、Safari、Opera 中不起作用?

java - 为坐标对的二维数组返回一维数组的整数索引

java - 什么是间接递归?

javascript - 在这种情况下,document.createElement 或 document.write 哪个更好?