admin管理员组文章数量:1302381
I have a JSON tree structure like this.
[
{
"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"
}
]
}
]
}
]
What i want is when i pass "northeast" to a function() it should return me the dot notation string path from the root. Expected return string from the function in this case would be "links.world.usa.northeast"
I have a JSON tree structure like this.
[
{
"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"
}
]
}
]
}
]
What i want is when i pass "northeast" to a function() it should return me the dot notation string path from the root. Expected return string from the function in this case would be "links.world.usa.northeast"
Share edited Jan 13, 2020 at 8:54 Eddie 26.9k6 gold badges38 silver badges59 bronze badges asked Jan 13, 2020 at 8:54 Sakti DashSakti Dash 751 silver badge5 bronze badges 5- please post your effort (code) to achieve this. – Ahmed Ali Commented Jan 13, 2020 at 8:57
- nested parent path? you'll need to explain what you mean by that – Jaromanda X Commented Jan 13, 2020 at 8:58
- Objects are not aware of their "parents", since objects don't have a parent. You've to mark the parent as a property in the objects having a "parent". – Teemu Commented Jan 13, 2020 at 9:00
- Are you trying to implement a binary tree? – arizafar Commented Jan 13, 2020 at 9:01
- Please add the code you've tried. There are many similar questions: Get parent and grandparent keys from a value inside a deep nested object and Javascript - Find path to object reference in nested object and Find a full object path to a given value with JavaScript – adiga Commented Jan 13, 2020 at 9:05
1 Answer
Reset to default 13You could test each nested array and if found, take the id
from every level as path.
const pathTo = (array, target) => {
var result;
array.some(({ id, children = [] }) => {
if (id === target) return result = id;
var temp = pathTo(children, target)
if (temp) return result = id + '.' + temp;
});
return result;
};
var data = [{ 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" }] }] }];
console.log(pathTo(data, 'northeast'));
本文标签: How to get nested parent path from a JSON tree in JavaScriptStack Overflow
版权声明:本文标题:How to get nested parent path from a JSON tree in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741703187a2393412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论