Javascript 在nodeJS应用程序中打印我的组树时出现问题
我正在尝试打印所有创建的组,它们都是子组,因此看起来是这样的:Javascript 在nodeJS应用程序中打印我的组树时出现问题,javascript,node.js,printing,tree,Javascript,Node.js,Printing,Tree,我正在尝试打印所有创建的组,它们都是子组,因此看起来是这样的: [ [ 'Father1', 'Child1', 'Child2', 'Child3' ], [ 'Father1', 'Child1', 'Child4' ], [ 'Father1', 'Child1', 'Child5' ] ] --root ---FooFather ----BarSemiFather -----FooChild ------BarBaby 我遇到的问题多种多样。发件人: var keys=nam
[ [ 'Father1', 'Child1', 'Child2', 'Child3' ],
[ 'Father1', 'Child1', 'Child4' ],
[ 'Father1', 'Child1', 'Child5' ] ]
--root
---FooFather
----BarSemiFather
-----FooChild
------BarBaby
我遇到的问题多种多样。发件人:
var keys=name.keys(o);^TypeError:name.keys不是总堆栈溢出的函数,iv'e调试了printPath函数,它正在单独完成它的工作,但不是使用我的最终树结构
我的树和打印函数如下所示:
[ [ 'Father1', 'Child1', 'Child2', 'Child3' ],
[ 'Father1', 'Child1', 'Child4' ],
[ 'Father1', 'Child1', 'Child5' ] ]
--root
---FooFather
----BarSemiFather
-----FooChild
------BarBaby
groups.js:
class groups {
constructor() {
this.root = new Group('root');
}
printPath(name){
this.root.getPath(name)
}
class Group {
constructor(name, parent) {
this.name = name;
this.parent = parent || null;
this.children = [];
this.users = new users || null;
}
getPath(name) {
function iter(o, p) {
var keys = name.keys(o);
if (keys.length) {
return keys.forEach(function (k) {
iter(o[k], p.concat(k));
});
}
result.push(p);
}
var result = [];
iter(name, []);
return result;
}
function createGroup(callback) {
rl.question('Add name for father group: \n', (parent) => {
let parentGroup = programdata.groups.findGroupByName(parent);
if (!parentGroup) {
parentGroup = programdata.groups.root;
}
rl.question('name of new group\n', (groupName) => {
parentGroup.setChildren(new Group(groupName, parentGroup));
console.log(parentGroup);
callback();
});
})
}
setChildren(child) {
this.children.push(child);
}
group.js:
class groups {
constructor() {
this.root = new Group('root');
}
printPath(name){
this.root.getPath(name)
}
class Group {
constructor(name, parent) {
this.name = name;
this.parent = parent || null;
this.children = [];
this.users = new users || null;
}
getPath(name) {
function iter(o, p) {
var keys = name.keys(o);
if (keys.length) {
return keys.forEach(function (k) {
iter(o[k], p.concat(k));
});
}
result.push(p);
}
var result = [];
iter(name, []);
return result;
}
function createGroup(callback) {
rl.question('Add name for father group: \n', (parent) => {
let parentGroup = programdata.groups.findGroupByName(parent);
if (!parentGroup) {
parentGroup = programdata.groups.root;
}
rl.question('name of new group\n', (groupName) => {
parentGroup.setChildren(new Group(groupName, parentGroup));
console.log(parentGroup);
callback();
});
})
}
setChildren(child) {
this.children.push(child);
}
编辑:
对于创建组,我使用菜单处理函数:
class groups {
constructor() {
this.root = new Group('root');
}
printPath(name){
this.root.getPath(name)
}
class Group {
constructor(name, parent) {
this.name = name;
this.parent = parent || null;
this.children = [];
this.users = new users || null;
}
getPath(name) {
function iter(o, p) {
var keys = name.keys(o);
if (keys.length) {
return keys.forEach(function (k) {
iter(o[k], p.concat(k));
});
}
result.push(p);
}
var result = [];
iter(name, []);
return result;
}
function createGroup(callback) {
rl.question('Add name for father group: \n', (parent) => {
let parentGroup = programdata.groups.findGroupByName(parent);
if (!parentGroup) {
parentGroup = programdata.groups.root;
}
rl.question('name of new group\n', (groupName) => {
parentGroup.setChildren(new Group(groupName, parentGroup));
console.log(parentGroup);
callback();
});
})
}
setChildren(child) {
this.children.push(child);
}
findGroupByName是我制作的一个很好的递归,它可以在类组中查找嵌套组(请随意使用!)。
findGroupByName(name) {
if (!name) return null;
return this._findGroupByNameInternal(this.root, name);
}
_findGroupByNameInternal(group, name) {
if (!group) return null;
if (group.name === name) return group;
for (const g of group.children) {
const result = this._findGroupByNameInternal(g, name);
if (!result) continue;
return result;
}
}
和setChildren函数放置在类组中:
class groups {
constructor() {
this.root = new Group('root');
}
printPath(name){
this.root.getPath(name)
}
class Group {
constructor(name, parent) {
this.name = name;
this.parent = parent || null;
this.children = [];
this.users = new users || null;
}
getPath(name) {
function iter(o, p) {
var keys = name.keys(o);
if (keys.length) {
return keys.forEach(function (k) {
iter(o[k], p.concat(k));
});
}
result.push(p);
}
var result = [];
iter(name, []);
return result;
}
function createGroup(callback) {
rl.question('Add name for father group: \n', (parent) => {
let parentGroup = programdata.groups.findGroupByName(parent);
if (!parentGroup) {
parentGroup = programdata.groups.root;
}
rl.question('name of new group\n', (groupName) => {
parentGroup.setChildren(new Group(groupName, parentGroup));
console.log(parentGroup);
callback();
});
})
}
setChildren(child) {
this.children.push(child);
}
编辑:
谢谢你的回答,你能帮我在我的菜单处理程序中实现你的方法吗?我试过这个,但它什么也没给我
function createGroup(callback) {
rl.question('Add name for father group: \n', (parent) => {
let parentGroup = programdata.groups.findGroupByName(parent);
let treePath = Group.root.printPath();
if (!parentGroup) {
parentGroup = programdata.groups.root;
}
rl.question('name of new group\n', (groupName) => {
parentGroup.addChild(new Group(groupName, parentGroup));
console.log(treePath);
callback();
});
})
}
出现错误的根本原因是字符串作为参数
name
传递到getPath(name)
中,您知道JS string对象没有函数属性keys
我重构了您的代码并修复了一些错误,这是可测试的版本。请将它们放在同一个文件夹中并运行test.js
group.js
groups.js
test.js
输出为:
享受它:)好的,找到了解决方案
Treeshow(){
var node = this.root;
var depth = '-'
recurse( node );
function recurse( node) {
depth +='-'
console.log(depth+node.name);
for (var child in node.children ) {
recurse(node.children[child]);
}
depth = depth.slice(0, -1);
}
}
这将显示我的树,如下所示:
[ [ 'Father1', 'Child1', 'Child2', 'Child3' ],
[ 'Father1', 'Child1', 'Child4' ],
[ 'Father1', 'Child1', 'Child5' ] ]
--root
---FooFather
----BarSemiFather
-----FooChild
------BarBaby
请提供一些可测试的输入(您是如何创建对象的),因为我似乎无法通过阅读这些代码来发现。谢谢,我更新了我的帖子。@Anatsu,我不知道什么是
rl。问题
和programdata
您是对的,我没有解释这一点,programdata是user,users,组:const programdata={用户:新用户(),组:新组(),组:新组()};在rl.question中,我调用readline来接受字符串输入:当然,我在主js文件的开头调用它。const readline=require('readline');const rl=readline.createInterface({输入:process.stdin,输出:process.stdout});