【发布时间】:2019-02-13 17:47:40
【问题描述】:
我需要一个打字稿函数,通过传递给定节点的值来递归获取给定节点的完整路径(父层次结构)。
假设我有一个这样的对象数组:
items = [{
value: 2,
text: "Name 2",
children: [{
value: 7,
text: "Name 7",
children: [{
value: 10,
text: "Name 10",
children: []
},
{
value: 11,
text: "Name 11",
children: []
},
{
value: 12,
text: "Name 12",
children: [{
value: 13,
text: "Name 13",
children: [{
value: 14,
text: "Name 14",
children: []
},
{
value: 15,
text: "Name 15",
children: []
}
]
}]
}
]
}]
},
{
value: 16,
text: "Name 16",
children: [{
value: 17,
text: "Name 17",
children: [{
value: 18,
text: "Name 18",
children: []
},
{
value: 19,
text: "Name 19",
children: []
}
]
}]
}
];
假设我想通过调用一个函数来获取 value=19 的节点的完整路径。
getPath(items, 19);
预期的结果可能是只返回父节点的值
[16, 17, 19]
或对象数组如下:
[{ 值:16, 文本:“名称 16” }, { 值:17, 文本:“姓名 17” }, { 值:19, 文字:“姓名 19” } ]
谢谢,
【问题讨论】:
-
你的代码出了什么问题?
-
如果你能分享你已经拥有的代码并指出它在哪里不起作用会很有帮助。
-
@NinaScholz 没有错!我只需要一个函数如何获取给定节点的完整路径。
标签: javascript typescript