- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个 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。
最佳答案
"In the end i need that list to be so that whenever there is a children element, it's name should have an extra
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 + " " // 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
}
];
<div></div>
关于Javascript使用递归从给定数组生成数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46624846/
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!