在 json 中搜索多维对象时如何跟踪对象父级?

发布于 2024-10-26 03:11:16 字数 2401 浏览 2 评论 0原文

我实现搜索的功能如下。我遇到的问题是我需要跟踪必须经过哪些行才能找到 URL。我正在构建一个导航“小部件”,我需要它根据 URL 扩展到正确的位置。鉴于 URL 可能有 N 行深,我需要一种方法来跟踪它所经过的行。

EG: row[1].tree.row[3].tree.row[0] ,这样我知道扩展第二个元素的导航,然后是第四个元素,然后突出显示该列表中的第一个元素。

问题在于我传回函数的 rowNum = rowNum+"x"+x; 。我想当我认为这会起作用时我可能会过度疲劳,但我没有想清楚。

建议?

谢谢! 我对这个相同的功能还有另一个问题,但这个问题是不同的。提交附加问题是否不妥?

    function lmIntra_LeftNavBuilder_findURL(url)
{

    return lmIntra_LeftNavBuilder_searchJson(jsonLNav.tree[0],url,null);
}//end findURL

function lmIntra_LeftNavBuilder_searchJson(tree,url,rowNum)
{
    if(rowNum == null)
    {
        rowNum="";
    }
    for(var x=0; x<=tree.rows.length-1;x++)
    {

    var cururl = "";
        if(typeof tree.rows[x] ==="undefined")
        {
            cururl="";
        }else
        {
            var cururl = tree.rows[x].url;
        }
        if(url == cururl )
        {
            //return tree.rows[x].title;
            return  rowNum + " treeDepth:"+tree.pos;
        }//end if
        else
            {

                if(typeof tree.rows[x]!= "undefined")
                {
                    if(typeof tree.rows[x].tree.rows != "undefined")
                    {
                    rowNum = rowNum+"x"+x;
                        var t = lmIntra_LeftNavBuilder_searchJson( tree.rows[x].tree,url,rowNum);
                        if (t) return t;
                    }//end if
                }//end if typeof tree.rows[x].tree!= "undefined"
            }//end else 

    }//end for


}//end searchJson

这是一个更简单的 json 对象。它已经完全成形,只是没有深度。完整的有 38K 个字符,所以我将其省略。

var jsonLNav = {itemClassName:"NodeLink",linkClassName:"NodeLinkTitle",linkHideClassName:"HideFromProd",navCategoryClassName:"NavCategory",onLoadJS:"",tree:[{pos:1,wid:"263a97c2-7cb9-470c-bf86-cadc28ae1323",pid:"1",rows:[{hide:0,title:"More IT Help",isNC:0,isMig:0,url:"http://vm-hsspdv-d09p/en-us/Help/Pages/ITHelp.aspx",isOL:0,tree:{pos:2,wid:"263a97c2-7cb9-470c-bf86-cadc28ae1323",pid:"3"}},{hide:0,title:"Office 2010",isNC:0,isMig:1,url:"http://office2010.lmig.com/Pages/Default.aspx",isOL:0,tree:{pos:2,wid:"263a97c2-7cb9-470c-bf86-cadc28ae1323",pid:"9"}},{hide:0,title:"E-mail Management",isNC:0,isMig:0,url:"http://vm-hsspdv-d09p/en-us/Help/EmailManagement/Pages/default.aspx",isOL:0,tree:{pos:2,wid:"8be66348-8da1-4e5c-90c5-0930d2f52d1a",pid:"123"}},]}]}; 

My function to implement the search is below. The issue I have is I need to track what rows I have to go through to find the URL. I'm building a navigation "widget" and I need it to expand to the correct place based on the URL. Seeing as the URL could be N rows deep, I need a method to track the rows that it passed through.

E.G: row[1].tree.row[3].tree.row[0] , this way I know to expand the navigation for the second element, then the fourth element, then highlight the first element in that list.

The issue is with the rowNum = rowNum+"x"+x; that I pass back to the function. I think I might be overtired when I thought that would work, I didn't think it through.

Suggestions?

Thanks!
I had another question out there about this same function, but this question is different. Is it bad form to submit an additional question?

    function lmIntra_LeftNavBuilder_findURL(url)
{

    return lmIntra_LeftNavBuilder_searchJson(jsonLNav.tree[0],url,null);
}//end findURL

function lmIntra_LeftNavBuilder_searchJson(tree,url,rowNum)
{
    if(rowNum == null)
    {
        rowNum="";
    }
    for(var x=0; x<=tree.rows.length-1;x++)
    {

    var cururl = "";
        if(typeof tree.rows[x] ==="undefined")
        {
            cururl="";
        }else
        {
            var cururl = tree.rows[x].url;
        }
        if(url == cururl )
        {
            //return tree.rows[x].title;
            return  rowNum + " treeDepth:"+tree.pos;
        }//end if
        else
            {

                if(typeof tree.rows[x]!= "undefined")
                {
                    if(typeof tree.rows[x].tree.rows != "undefined")
                    {
                    rowNum = rowNum+"x"+x;
                        var t = lmIntra_LeftNavBuilder_searchJson( tree.rows[x].tree,url,rowNum);
                        if (t) return t;
                    }//end if
                }//end if typeof tree.rows[x].tree!= "undefined"
            }//end else 

    }//end for


}//end searchJson

Here's a simpler json object. It's fully formed, it just doesn't have the depth. The full one is 38K characters, so I'll leave it out.

var jsonLNav = {itemClassName:"NodeLink",linkClassName:"NodeLinkTitle",linkHideClassName:"HideFromProd",navCategoryClassName:"NavCategory",onLoadJS:"",tree:[{pos:1,wid:"263a97c2-7cb9-470c-bf86-cadc28ae1323",pid:"1",rows:[{hide:0,title:"More IT Help",isNC:0,isMig:0,url:"http://vm-hsspdv-d09p/en-us/Help/Pages/ITHelp.aspx",isOL:0,tree:{pos:2,wid:"263a97c2-7cb9-470c-bf86-cadc28ae1323",pid:"3"}},{hide:0,title:"Office 2010",isNC:0,isMig:1,url:"http://office2010.lmig.com/Pages/Default.aspx",isOL:0,tree:{pos:2,wid:"263a97c2-7cb9-470c-bf86-cadc28ae1323",pid:"9"}},{hide:0,title:"E-mail Management",isNC:0,isMig:0,url:"http://vm-hsspdv-d09p/en-us/Help/EmailManagement/Pages/default.aspx",isOL:0,tree:{pos:2,wid:"8be66348-8da1-4e5c-90c5-0930d2f52d1a",pid:"123"}},]}]}; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凑诗 2024-11-02 03:11:16

不过,如果你真的想坚持现有的方法,我认为这并不是太遥远的事情。如果我明白你想要什么,那么最大的问题是你需要做类似的事情:

    if(url == cururl )
    {
        rowNum = rowNum+"x"+x;
        return  rowNum + " treeDepth:"+tree.pos;
    }

If you really want to stick with the approach you have, though, I don't think it's really too far off. If I understand what you want, the biggest problem is that you need to do something like:

    if(url == cururl )
    {
        rowNum = rowNum+"x"+x;
        return  rowNum + " treeDepth:"+tree.pos;
    }
梦过后 2024-11-02 03:11:16

想必这棵树中存在的所有内容都会映射到 DOM 中存在的内容,对吗?我认为最明智的选择是停止遍历这个对象来查找你想要的东西,使用像 jQuery 这样的库和选择器引擎来选择你想要的节点,然后使用所述库来遍历备份 DOM。即使在没有库的情况下遍历 DOM 对您来说也可能更容易。

Presumably everything that exists in this tree maps to something that exists in the DOM, right? I think the most sensible option would be to stop traversing this object to find what you want, use a library like jQuery with a selector engine to select the node you want, and then use said library to traverse back up the DOM. Even traversing the DOM without a library might be easier for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文