如何从嵌套JSON中获取含父级的完整符合flag条件的对象?
问题描述
我有一个嵌套结构的JSON数据,当前页面的link值需要在这个JSON里搜索,找到对应的顶级对象(即pages数组里的元素),如果该顶级对象的flag为true,就返回这个包含所有子层级的完整顶级对象。比如当前页面是sample3a2.html时,要返回flag为true的sample3.html对应的完整对象。我用递归函数尝试实现,但只能返回当前层级的对象,拿不到包含父级、祖父级的完整顶级对象。
原始JSON结构
{ "pages": [ { "link": "sample.html", "flag": false, "child1": [] }, { "link": "sample2.html", "flag": false, "child1": [ { "link": "sample2a.html", "child2": [] } ] }, { "link": "sample3.html", "flag": true, "child1": [ { "link": "sample3a.html", "child2": [ { "link": "sample3a1.html" }, { "link": "sample3a2.html" }, { "link": "sample3a3.html" } ] }, { "link": "sample3b.html", "child2": [ { "link": "sample3b1.html" }, { "link": "sample3b2.html" }, { "link": "sample3b3.html" } ] } ] } ] }
期望输出
{ "link": "sample3.html", "flag": true, "child1": [ { "link": "sample3a.html", "child2": [ { "link": "sample3a1.html" }, { "link": "sample3a2.html" }, { "link": "sample3a3.html" } ] }, { "link": "sample3b.html", "child2": [ { "link": "sample3b1.html" }, { "link": "sample3b2.html" }, { "link": "sample3b3.html" } ] } ] }
尝试的代码
JavaScript函数
function checkFlag(pages, currentPageUrl) { function searchInObject(obj) { if (obj && typeof obj === 'object') { if (obj.link === currentPageUrl) { console.log("obj.link", obj) return obj; } for (const key in obj) { const result = searchInObject(obj[key]); if (result !== undefined) { return result; } } } return undefined; } for (const page of pages) { const result = searchInObject(page); if (result !== undefined) { return result; } } return undefined; } let currentURL = "sample3a2.html" checkFlag(pages, currentURL)
HTML中的数据定义
<script> const pages = [{ "link": "sample.html", "flag": true, "child1": [] }, { "link": "sample2.html", "flag": false, "child1": [{ "link": "sample2a.html", "child2": [] }] }, { "link": "sample3.html", "flag": true, "child1": [{ "link": "sample3a.html", "child2": [{ "link": "sample3a1.html" }, { "link": "sample3a2.html" }, { "link": "sample3a3.html" } ] }, { "link": "sample3b.html", "child2": [{ "link": "sample3b1.html" }, { "link": "sample3b2.html" }, { "link": "sample3b3.html" } ] } ] } ] </script>
解决方案
原代码的问题是递归找到目标子节点后直接返回了该节点,没有回溯到顶级父对象。需要调整逻辑:遍历每个顶级页面,检查该页面的整个嵌套结构中是否包含目标link,如果包含且该顶级页面的flag为true,就返回这个顶级页面。
修改后的代码如下:
function findMatchingTopPage(pages, targetLink) { // 递归检查当前对象或其子孙节点是否包含目标link function hasTargetLink(obj) { if (!obj || typeof obj !== 'object') return false; if (obj.link === targetLink) return true; // 遍历所有属性,检查子节点 for (const key in obj) { if (hasTargetLink(obj[key])) { return true; } } return false; } // 遍历每个顶级页面 for (const page of pages) { if (page.flag && hasTargetLink(page)) { return page; } } return undefined; } // 调用示例 const currentURL = "sample3a2.html"; const result = findMatchingTopPage(pages, currentURL); console.log(result);
代码说明
- 内部函数
hasTargetLink负责递归检查某个对象及其所有子节点中是否存在目标link值。 - 外层遍历
pages数组中的每个顶级页面,先判断该页面的flag是否为true,再用hasTargetLink检查该页面的嵌套结构里是否包含目标链接。 - 一旦找到符合条件的顶级页面,直接返回它,这样就能得到完整的顶级对象及其所有子层级。
内容的提问来源于stack exchange,提问作者user3699344
相关产品推荐
相关产品推荐

