【发布时间】:2019-08-29 13:19:13
【问题描述】:
我有一个深度嵌套的 JavaScript 对象,我知道一个值位于对象的最低位置。我想知道父键和祖父键。
我设置了一个jsbin https://jsbin.com/yimugezuxe/edit?js,console
基于值“uniqueID#9aserdf”,我想知道/返回其父键:来自下面对象的“Stage 2”和“Level 2”:
const obj = {
"Level 1": {
"Stage 1": [
{
"title": "Parent 1",
"id": "Parent1#id",
"Children": [
{
"title": "Steve",
"id": "uniqueID1"
},
{
"title": "James",
"id": "uniqueID#9"
}
]
}
]
},
"Level 2": {
"Stage 1": [
{
"title": "Parent 4",
"id": "Parent4#id",
"Children": [
{
"title": "Tim",
"id": "uniqueIDadsf"
},
{
"title": "Hans",
"id": "uniqueID#9asdf"
}
]
}
],
"Stage 2": [
{
"title": "Parent 10",
"id": "Parent10#id",
"Children": [
{
"title": "Chris",
"id": "uniqueIDadsf33"
},
{
"title": "Jack",
"id": "uniqueID#9aserdf"
}
]
}
]
}
};
// based on the value 'uniqueID#9aserdf' I want to know/return its parent keys: 'Stage 2' and 'Level 2'
【问题讨论】:
-
请发布您已经尝试过的代码,以便我们为您提供帮助。
-
我正在尝试 for..in 循环,但由于它嵌套在第二级,我感到困惑。我需要嵌套 for..in 循环吗?
-
答案与结构无关。我发布了一个答案,它调整了两个链接的答案,几乎没有更改代码...
标签: javascript json object