如何在 javascript 中将嵌套集转换为嵌套数组?
How to convert nested set into nested array in javascript?
有如下数据。
[
{"no":1, "name":"ELECTRONICS", "depth":0},
{"no":2, "name":"TELEVISIONS", "depth":1},
{"no":3, "name":"TUBE", "depth":2},
{"no":4, "name":"LCD", "depth":2},
{"no":5, "name":"PLASMA", "depth":2},
{"no":6, "name":"PORTABLE ELECTRONICS", "depth":1},
{"no":7, "name":"MP3 PLAYERS", "depth":2},
{"no":8, "name":"FLASH", "depth":3},
{"no":9, "name":"CD PLAYERS", "depth":2},
{"no":10, "name":"2 WAY RADIOS", "depth":2}
]
我想获取如下数据。
[
{
"no":1,
"name":"ELECTRONICS",
"depth":0,
"child_nodes":[
{
"no":2,
"name":"TELEVISIONS",
"depth":1
"child_nodes":[
{
"no":3,
"name":"TUBE",
"depth":2
},
...
]
},
{
"no":6,
"name":"PORTABLE ELECTRONICS",
"depth":1
"child_nodes":[ ... ]
}
]
}
]
我正在递归尝试,但效果不好。由于我用的是babel,所以对javascript的新功能没有太大的限制。如果你有好的想法,请告诉我。谢谢!
//The trees root = our expected result
const result = [];
var acc = { depth: -1, children: result};
for(const el of data){
//walk upwards in the tree
var up = acc.depth - el.depth + 1 ;
while(up--){ acc = acc.parent }
//walk down and add the current el as a child
el.parent = acc;
(acc.children || (acc.children = [])).push(el);
acc = el;
}
console.log(result);
您可以一起穿过一棵树 (acc) 和 link parents/children。
您可以为关卡使用辅助数组。
var array = [{ no: 1, name: "ELECTRONICS", depth: 0 }, { no: 2, name: "TELEVISIONS", depth: 1 }, { no: 3, name: "TUBE", depth: 2 }, { no: 4, name: "LCD", depth: 2 }, { no: 5, name: "PLASMA", depth: 2 }, { no: 6, name: "PORTABLE ELECTRONICS", depth: 1 }, { no: 7, name: "MP3 PLAYERS", depth: 2 }, { no: 8, name: "FLASH", depth: 3 }, { no: 9, name: "CD PLAYERS", depth: 2 }, { no: 10, name: "2 WAY RADIOS", depth: 2 }],
result = [],
levels = [{ children: result }];
array.forEach(function (o) {
levels[o.depth].children = levels[o.depth].children || [];
levels[o.depth].children.push(levels[o.depth + 1] = o);
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
有如下数据。
[
{"no":1, "name":"ELECTRONICS", "depth":0},
{"no":2, "name":"TELEVISIONS", "depth":1},
{"no":3, "name":"TUBE", "depth":2},
{"no":4, "name":"LCD", "depth":2},
{"no":5, "name":"PLASMA", "depth":2},
{"no":6, "name":"PORTABLE ELECTRONICS", "depth":1},
{"no":7, "name":"MP3 PLAYERS", "depth":2},
{"no":8, "name":"FLASH", "depth":3},
{"no":9, "name":"CD PLAYERS", "depth":2},
{"no":10, "name":"2 WAY RADIOS", "depth":2}
]
我想获取如下数据。
[
{
"no":1,
"name":"ELECTRONICS",
"depth":0,
"child_nodes":[
{
"no":2,
"name":"TELEVISIONS",
"depth":1
"child_nodes":[
{
"no":3,
"name":"TUBE",
"depth":2
},
...
]
},
{
"no":6,
"name":"PORTABLE ELECTRONICS",
"depth":1
"child_nodes":[ ... ]
}
]
}
]
我正在递归尝试,但效果不好。由于我用的是babel,所以对javascript的新功能没有太大的限制。如果你有好的想法,请告诉我。谢谢!
//The trees root = our expected result
const result = [];
var acc = { depth: -1, children: result};
for(const el of data){
//walk upwards in the tree
var up = acc.depth - el.depth + 1 ;
while(up--){ acc = acc.parent }
//walk down and add the current el as a child
el.parent = acc;
(acc.children || (acc.children = [])).push(el);
acc = el;
}
console.log(result);
您可以一起穿过一棵树 (acc) 和 link parents/children。
您可以为关卡使用辅助数组。
var array = [{ no: 1, name: "ELECTRONICS", depth: 0 }, { no: 2, name: "TELEVISIONS", depth: 1 }, { no: 3, name: "TUBE", depth: 2 }, { no: 4, name: "LCD", depth: 2 }, { no: 5, name: "PLASMA", depth: 2 }, { no: 6, name: "PORTABLE ELECTRONICS", depth: 1 }, { no: 7, name: "MP3 PLAYERS", depth: 2 }, { no: 8, name: "FLASH", depth: 3 }, { no: 9, name: "CD PLAYERS", depth: 2 }, { no: 10, name: "2 WAY RADIOS", depth: 2 }],
result = [],
levels = [{ children: result }];
array.forEach(function (o) {
levels[o.depth].children = levels[o.depth].children || [];
levels[o.depth].children.push(levels[o.depth + 1] = o);
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }