假设我有一个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-cycles。
答案 0 :(得分:2)
&#34;最后我需要该列表,以便每当有子元素时,它的名称前面应该有一个额外的
与之相比#39;父母。&#34;
您需要做的就是传入一个字符串,并在每次递归调用时添加它。
编辑:我错过了你错误地处理了递归。该函数需要一个数组,以便您需要传递的内容。不需要内循环。
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 + " " // 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 + " " // 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
}
];
&#13;
<div></div>
&#13;