如何在JavaScript中从JSON树获取嵌套的父路径?
我有一个类似这样的JSON树结构如何在JavaScript中从JSON树获取嵌套的父路径?,javascript,json,treeview,parent-child,Javascript,Json,Treeview,Parent Child,我有一个类似这样的JSON树结构 [ { "title":"News", "id":"news" }, { "title":"Links", "id":"links", "children":[ { "title":"World", "id":"world", "children":[ {
[
{
"title":"News",
"id":"news"
},
{
"title":"Links",
"id":"links",
"children":[
{
"title":"World",
"id":"world",
"children":[
{
"title":"USA",
"id":"usa",
"children":[
{
"title":"Northeast",
"id":"northeast"
},
{
"title":"Midwest",
"id":"midwest"
}
]
},
{
"title":"Europe",
"id":"europe"
}
]
}
]
}
]
我想要的是,当我将“northeast”传递给函数()时,它应该从根返回点符号字符串路径。在本例中,函数的预期返回字符串为“links.world.usa.northeast”您可以测试每个嵌套数组,如果找到,则将每个级别的
id
作为路径
constpathto=(数组,目标)=>{
var结果;
array.some({id,children=[]})=>{
如果(id==target)返回结果=id;
var temp=pathTo(子级,目标)
如果(温度)返回结果=id+'.+temp;
});
返回结果;
};
var数据=[{title:“新闻”,id:“新闻”},{title:“链接”,id:“链接”,儿童:[{title:“世界”,id:“世界”,儿童:[{title:“美国”,id:“美国”,儿童:[{{title:“东北”,id:“中西部”},{title:“欧洲”}];
log(路径(数据“东北”)代码>请发布您为实现此目标所做的努力(代码)。嵌套父路径?您需要解释对象不知道其“父对象”是什么意思,因为对象没有父对象。您必须将父对象标记为具有“父对象”的对象中的属性。是否尝试实现二叉树?请添加您尝试过的代码。有许多类似的问题:和