我有一个类似这样的JSON数据,并且我需要获取内部大多数孩子的数据,即 hierarchyLevel:4 。而且这个JSON数据不是静态的,hierarchyLevel可以做任何事情,比如5,6,7。 请帮助在javascript中找到解决方案。
{
"hierarchylist": [
{
"hierarchyId": 10,
"hierarchyLevel": 0,
"name": "ABC",
"parentId": 0,
"children": [
{
"hierarchyId": 12,
"hierarchyLevel": 1,
"name": "ABC-Child1",
"parentId": 10,
"children": [
{
"hierarchyId": 2,
"hierarchyLevel": 2,
"name": "People Management & Development1 ",
"parentId": 12,
"children": [
{
"hierarchyId": 5,
"hierarchyLevel": 3,
"name": "Resourcing2_1",
"parentId": 2,
"children": [
{
"hierarchyId": 19,
"hierarchyLevel": 4,
"name": "Resource Request ",
"parentId": 5,
"children": [],
"docId": 19,
"docstatusid": 20
}
]
}
]
}
]
}
]
}
]
}
感谢。
答案 0 :(得分:1)
这应该让你成为最内在的人:
let data = {
"hierarchylist": [
{
"hierarchyId": 10,
"hierarchyLevel": 0,
"name": "ABC",
"parentId": 0,
"children": [
{
"hierarchyId": 12,
"hierarchyLevel": 1,
"name": "ABC-Child1",
"parentId": 10,
"children": [
{
"hierarchyId": 2,
"hierarchyLevel": 2,
"name": "People Management & Development1 ",
"parentId": 12,
"children": [
{
"hierarchyId": 5,
"hierarchyLevel": 3,
"name": "Resourcing2_1",
"parentId": 2,
"children": [
{
"hierarchyId": 19,
"hierarchyLevel": 4,
"name": "Resource Request ",
"parentId": 5,
"children": [],
"docId": 19,
"docstatusid": 20
}
]
}
]
}
]
}
]
}
]
}
let children = data.hierarchylist[0].children;
while(children[0] && children[0].children && children[0].children.length) {
children = children[0].children;
}
console.log(children);
答案 1 :(得分:0)
您可以尝试使用while循环,这是示例
var children = json['hierarchylist']['children'][0];
while(typeof children !== 'undefined') {
children = children['children'][0];
}
console.log(children);