老师,我用堆栈的方式实现了递归,但是这样一来我就丢失了树层级结构的相关信息,这个如何解决?
var stack = scope.option;
scope.pid = null;
while (stack.length) {
var n = stack.shift();
// n.id = scope.index ++;
// n.pid = scope.pid;
// if (n.id == 0) {
// scope.folders = [gui.addFolder(n.label)]
// }
// 若是最底层节点
if (!n.children) {
// n.pid = scope.pid;
// scope.pid = n.id;
// var d = { [n.id]: n.value };
console.log('文件')
// scope.folders[scope.folders.length - 1].add(d, n.id, n.min, n.max, n.value);
continue;
}
if (n.children) {
scope.pid = n.id;
// stack.push(...getChildAtPid(n));
stack.push(...n.children)
// scope.folders = []
console.log('文件夹', n.label, stack);
}
console.log('n', n)
}
option: [{ label: '场景', prop: 'all', children: [
{ label: '全景图', prop: 'pano', children: [
{ label: 'x', prop: 'x', value: 500, min: 0, max: 1000 },
{ label: 'y', prop: 'y', value: 500, min: 0, max: 1000 },
{ label: 'z', prop: 'z', value: 500, min: 0, max: 1000 },
{ label: 'root', prop: 'prop', children: []}
]},
{ label: '模型', prop: 'model', children: [
{ label: 'x', prop: 'x', value: 500, min: 0, max: 1000 },
{ label: 'y', prop: 'y', value: 500, min: 0, max: 1000 },
{ label: 'z', prop: 'z', value: 500, min: 0, max: 1000 }
]},
{ label: '摄像头', prop: 'camera', children: [
{ label: 'x', prop: 'x', value: 500, min: 0, max: 1000 },
{ label: 'y', prop: 'y', value: 500, min: 0, max: 1000 },
{ label: 'z', prop: 'z', value: 500, min: 0, max: 1000 }
]}
] }],
这样实现的目的是,我使用了google的dat.GUI生成树节点组件,也没想出什么办法,我现在是把 stack.push(…n.children)替换成了一个循环来记录每个子节点的父节点信息,两重循环感觉实现总不是太好,我又不想用递归来做这个事,有什么好办法解决这个事